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.
In the code you posted, *args simply indicates that the method accepts a variable number of arguments in an array called args . It could have been called anything you want (following the Ruby naming rules, of course).
As of Ruby 2.7 now, implicit hash parameters are deprecated and will be removed in Ruby 3.0 – means, we need again the {…} explicitly to pass a hash (see). The Ruby 1.9 syntax shown here is still possible for calling a method with keyword parameters.
Variable-length arguments, varargs for short, are arguments that can take an unspecified amount of input. When these are used, the programmer does not need to wrap the data in a list or an alternative sequence. In Python, varargs are defined using the *args syntax.
1. Non-keyword Variable Arguments (Tuple) When a function is invoked, all formal (required and default) arguments are assigned to their corresponding local variables as given in the function declaration. The remaining non-keyword variable arguments are inserted in order into a tuple for access.
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 to func
will be putting into an array called args
. You could also specify specific arguments before a variable-length argument like so:
def func2(arg1, arg2, *other_args) # ... end
Let's say we call this method:
func2(1, 2, 3, 4, 5)
If you inspect arg1
, arg2
and other_args
within func2
now, you will get the following results:
def func2(arg1, arg2, *other_args) p arg1.inspect # => 1 p arg2.inspect # => 2 p other_args.inspect # => [3, 4, 5] end
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:
my_func arg1, arg2, :html_arg => value, :html_arg2 => value2
is equivalent to
my_func(arg1, arg2, {:html_arg => value, :html_arg2 => value2})
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):
def my_func(arg1, arg2, html_args = {}) # ... end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With