Ruby - Methods
You want a Ruby method to return the first non-nil value from two expressions. Which method definition correctly uses Ruby's automatic return to achieve this?
def first_non_nil(a, b) # fill in end
def first_non_nil(a, b) # fill in end
a and b.a || b which evaluates to first non-nil value automatically. def first_non_nil(a, b)
return a if a
return b
end uses explicit returns but is longer. def first_non_nil(a, b)
puts a || b
end prints but returns nil. def first_non_nil(a, b)
a
b
end returns b always because last line is b.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions