Use the splat
operator:
def multiply(*arr)
arr.reduce(1, :*)
end
multiply(2, 3, 4, 5)
# => 120
If you want to also want to support input as an array, you can use flatten
on arr
:
def multiply(*arr)
arr.flatten.reduce(1, :*)
end
multiply([2, 3, 4, 5])
# => 120
multiply(10, 3, 5)
# => 150
multiply(10, 3)
# => 30