Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a hash to a function ( *args ) and its meaning

Tags:

ruby

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.

like image 902
shmichael Avatar asked Sep 13 '10 14:09

shmichael


People also ask

What does * args mean in Ruby?

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).

How do you pass hash as an argument in Ruby?

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.

What is variable length argument in Python?

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.

What is non keyword arguments in Python?

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.


1 Answers

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:

  • Variable Length Argument List, Asterisk Operator
  • What is the * operator doing

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 
like image 161
Daniel Vandersluis Avatar answered Oct 24 '22 13:10

Daniel Vandersluis