Search code examples
rubysplat

concatenate with splat ruby


say I have

arr = [1,2,3]

How can I change this method so it adds each argument to the array?

def add(*number)
  arr << *number
end

So add(4,5,6) produces:

arr #=> [1,2,3,4,5,6]

Solution

  • When accepting arguments via splat, they will always be an array. So you can simply add the two arrays together.

    def add(*numbers)
      arr + numbers
    end