instance method
Array#first
() -> Elem
(::int n) -> ::Array[Elem]
first → object or nil
first(count) → new_array
array.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/array.rb, line 129
def first n = unspecified = true
if Primitive.mandatory_only?
Primitive.attr! :leaf
Primitive.cexpr! %q{ ary_first(self) }
else
if unspecified
Primitive.cexpr! %q{ ary_first(self) }
else
Primitive.cexpr! %q{ ary_take_first_or_last_n(self, NUM2LONG(n), ARY_TAKE_FIRST) }
end
end
end
Returns elements from self, or nil; does not modify self.
With no argument given, returns the first element (if available):
Example
a = [:foo, 'bar', 2] a.first # => :foo a # => [:foo, "bar", 2]
If self is empty, returns nil.
Example
[].first # => nil
With a non-negative integer argument count given, returns the first count elements (as available) in a new array:
Example
a.first(0) # => [] a.first(2) # => [:foo, "bar"] a.first(50) # => [:foo, "bar", 2]
Related: see Methods for Querying.