In Ruby what's the most elegant way to write a method that accepts a group of arrays as arguments and returns the array with the most number of elements?
def largest_array(*arrays)
end
For example, given:
a1 = [*0..9]
a2 = [*0..99]
a3 = [*0..49]
a4 = [*0..19]
largest_array(a1, a2, a3, a4)
should return a2
.
Note that the method must return a reference to the largest array itself, not the size. Also, in this case it doesn't matter which array is returned if there are arrays with identical sizes.
Using Enumerable#max_by
:
def largest_array(*arrays)
arrays.max_by &:size
end
a1 = [*0..9]
a2 = [*0..99]
a3 = [*0..49]
a4 = [*0..19]
largest_array(a1, a2, a3, a4) == a2 # => true