Method: RangeExtd#first
- Defined in:
- lib/range_extd.rb
#first(*rest) ⇒ Object, Array
Like Range#last, if no argument is given, it behaves like #begin(), that is, it returns the initial value, regardless of #exclude_begin?.
If an argument is given (nb., acceptable since Ruby 1.9.2) when #exclude_begin? is true, it returns the array that starts from #begin().succ(), in the same way as Range#last with #exclude_end? of true.
The default behaviours are:
(1...3.1).last # => 3.1
(1...3.1).last(1) # => [3]
(1...3.0).last(1) # => [2]
(3.0..8).first(1) # raise: can't iterate from Float (TypeError)
580 581 582 583 584 585 586 587 |
# File 'lib/range_extd.rb', line 580 def first(*rest) if is_none? raise RangeError, "cannot get the first element of RangeExtd::NONE" end ran = _converted_rangepart(transform_to_nil: false, consider_exclude_begin: (1 == rest.size && exclude_begin?)) ran.send(__method__, *rest) end |