method
first
v2_6_3 -
Show latest stable
- Class:
Array
first(*args)public
Returns the first element, or the first n elements, of the array. If the array is empty, the first form returns nil, and the second form returns an empty array. See also Array#last for the opposite effect.
a = [ "q", "r", "s", "t" ] a.first #=> "q" a.first(2) #=> ["q", "r"]
2Notes
Extracting the First Element
To extract the first element from an Array, use shift:
array = [ 1, 2, 3 ] # => [ 1, 2, 3 ]
array.first # => 1
array # => [ 1, 2, 3 ]
array.shift # => 1
array # => [ 2, 3 ]