Answers for "ruby first method"

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