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

前端 未结 1 1368
甜味超标
甜味超标 2020-12-22 21:21

When using an idiom such as:

def func(*args)
  # some code
end

What is the meaning of *args? Googling this specific question w

1条回答
  •  佛祖请我去吃肉
    2020-12-22 21:23

    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
    

    0 讨论(0)
提交回复
热议问题