tags:

views:

385

answers:

3

Hi, How do I create a Ruby function that does not have an explicit number of parameters?

More clarification needed?

Thanks

+5  A: 

Use *rest. here's a nice little tutorial.

Charlie Martin
+3  A: 

Use the splat operator *

def foo(a,b,c,*others)
   # this function has at least three arguments,
   # but might have more
   puts a
   puts b
   puts c
   puts others.join(',')
end
foo(1,2,3,4,5,6,7,8,9)
# prints:
# 1
# 2
# 3
# 4,5,6,7,8,9
rampion
A: 

Here's another article on the subject:

www.misuse.org/science/2008/01/30/passing-multiple-arguments-in-ruby-is-your-friend

Gives some nice examples rolling and unrolling your parameters using "*"

science