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)

Parameters:

  • rest (Integer)

    Optional. Must be non-negative. Consult Range#first for detail.

Returns:

  • (Object)

    if no argument is given, equivalent to #end.

  • (Array)

    if an argument is given.

Raises:

  • (TypeError)

    if the argument (Numeric) is given and if #exclude_begin? is true, yet if #begin().succ is not defined, or yet if #is_none?

  • (RangeError)

    “cannot get the first element of beginless range” as per Range.

  • (ArgumentError)

    if more than 1 arguments are specified (delegated to Range)



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