本文簡要介紹ruby語言中 Range.last
的用法。
用法
last → object
last(n) → array
如果沒有參數,則返回 self
的最後一個元素(如果存在):
(1..4).last # => 4
('a'..'d').last # => "d"
請注意,即使 exclude_end?
是 true
,不帶參數的 last
也會返回 self
的結束元素:
(1...4).last # => 4
('a'...'d').last # => "d"
給定非負整數參數 n
,返回數組中最後的 n
元素:
(1..10).last(3) # => [8, 9, 10]
(1..10).last(0) # => []
(1..4).last(50) # => [1, 2, 3, 4]
請注意,如果 exclude_end?
它是 true
,則帶有參數的 last
不會返回 self
的結束元素:
(1...4).last(3) # => [1, 2, 3]
('a'...'d').last(3) # => ["a", "b", "c"]
如果沒有最後一個元素,則引發異常:
(1..).last # Raises RangeError
相關用法
- Ruby Range.end用法及代碼示例
- Ruby Range.size用法及代碼示例
- Ruby Range.new用法及代碼示例
- Ruby Range.min用法及代碼示例
- Ruby Range.max用法及代碼示例
- Ruby Range.exclude_end?用法及代碼示例
- Ruby Range.count用法及代碼示例
- Ruby Range.self ==用法及代碼示例
- Ruby Range.member?用法及代碼示例
- Ruby Range.eql?用法及代碼示例
- Ruby Range.begin用法及代碼示例
- Ruby Range.step用法及代碼示例
- Ruby Range.first用法及代碼示例
- Ruby Range.self ===用法及代碼示例
- Ruby Range.minmax用法及代碼示例
- Ruby Range.each用法及代碼示例
- Ruby Range.%用法及代碼示例
- Ruby Range.cover?用法及代碼示例
- Ruby Range.entries用法及代碼示例
- Ruby Range.to_s用法及代碼示例
- Ruby Range.to_a用法及代碼示例
- Ruby Range.include?用法及代碼示例
- Ruby Range.inspect用法及代碼示例
- Ruby Range new()用法及代碼示例
- Ruby Range last()用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Range.last。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。