Answers for "array.first ruby"

0

ruby get the first element of an array

array = [1, 2, 3]
array.first
=> 1
Posted by: Guest on March-16-2021
0

ruby .first

# 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.

a = [ "q", "r", "s", "t" ]
a.first     #=> "q"
a.first(2)  #=> ["q", "r"]

[46] pry(main)> a = []
=> []
[47] pry(main)> a.first
=> nil
[48] pry(main)> a.first(2)
=> []
Posted by: Guest on June-22-2021

Browse Popular Code Answers by Language