Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Function with Unlimited Number of Parameters

Tags:

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

More clarification needed?

like image 740
Devoted Avatar asked May 17 '09 03:05

Devoted


2 Answers

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

like image 27
Charlie Martin Avatar answered Sep 18 '22 20:09

Charlie Martin


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 
like image 70
rampion Avatar answered Sep 22 '22 20:09

rampion