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

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

Posted by: admin November 30, 2017 Leave a comment

Questions:

When using an idiom such as:

def func(*args)
  ...
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.

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:


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

Questions:
Answers:
def func(map={})
  x = map.delete(:y)
end