Hi, How do I create a Ruby function that does not have an explicit number of parameters?
More clarification needed?
Thanks
Hi, How do I create a Ruby function that does not have an explicit number of parameters?
More clarification needed?
Thanks
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
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 "*"