steve_gallagher
steve_gallagher

Reputation: 3888

Is there a way to input multiple parameters into a method via an array

Say I have a method that needs three arguments:

def foo(a, b, c)
end

And I have said arguments in an array:

[a, b, c]

Is there a trivial or one method way of using the array as the arguments, like:

foo(array.some_method)

Upvotes: 1

Views: 694

Answers (2)

pjam
pjam

Reputation: 6356

You can use the splat operator:

foo(*array)

Upvotes: 5

rovermicrover
rovermicrover

Reputation: 1453

def foo(*bar)

end

Foo now exacts an array as parameters. You could also make it accept a hash by doing

def foo(bar={})

end

Upvotes: 4

Related Questions