When using an idiom such as:
def func(*args)
# some code
end
What is the meaning of *args? Googling this specific question was pretty hard, and I couldn’t find anything.
It seems all the arguments actually appear in args[0] so I find myself writing defensive code such as:
my_var = args[0].delete(:var_name) if args[0]
But I’m sure there’s a better way I’m missing out on.
The
*is the splat (or asterisk) operator. In the context of a method, it specifies a variable length argument list. In your case, all arguments passed tofuncwill be putting into an array calledargs. You could also specify specific arguments before a variable-length argument like so:Let’s say we call this method:
If you inspect
arg1,arg2andother_argswithinfunc2now, you will get the following results:In your case, you seem to be passing a hash as an argument to your
func, in which case,args[0]will contain the hash, as you are observing.Resources:
Update based on OP’s comments
If you want to pass a Hash as an argument, you should not use the splat operator. Ruby lets you omit brackets, including those that specify a Hash (with a caveat, keep reading), in your method calls. Therefore:
is equivalent to
When Ruby sees the
=>operator in your argument list, it knows to take the argument as a Hash, even without the explicit{...}notation (note that this only applies if the hash argument is the last one!).If you want to collect this hash, you don’t have to do anything special (though you probably will want to specify an empty hash as the default value in your method definition):