For example:
def what_up(greeting, *bros)
bros.each { |bro| puts "#{greeting}, #{bro}!" }
end
what_up("What up", "Justin", "Ben", "Kevin Sorbo")
It's will print out 3 greetings like the following:I also faced the similar one in Python, which is called gather parameter . All arguments value will be collected into a tuple. To do this, you provide a final parameter definition of the form * args. The * indicates that this parameter variable is the place to capture the extra argument values. The variable, here called extras , will receive a sequence with all of the extra positional argument values.
For example:
def foo(*args):
print args
foo(1,2,3,4,5)
foo (1,False,"String")
You can try the above script quickly in terminal :
In Php, I haven't found anything like Splat arguments in Ruby. If you want to implement something like "splat arguments" in Php, I think : func_get_args is a good choice. This function will get the list of function's arguments and return an array.
For example:
function foo()
{
$arg_list = func_get_args();
var_export($arg_list);
}
echo "<pre>";
foo(1,2,3,4,5,6);
echo "</pre>
You can see the result like that:- http://www.codecademy.com/courses/ruby-beginner-en-ET4bU
- http://en.wikibooks.org/wiki/Think_Python/Tuples#Variable-length_argument_tuples
- http://de.php.net/manual/en/function.func-get-args.php