Reputation: 3164
I understand that **args
is interpreted as a hash containing all key value pairs passed to a function but I don't understand why that would be preferred over a typical parameter. For example, I have the following two functions.
def test(some_string, hash)
puts hash
puts hash.class # => Hash
end
def test_two(some_string, **hash)
puts hash
puts hash.class # => Hash
end
calling test("test string", a: 1, b: 2)
or test_two("test string", a: 1, b: 2)
produces the exact same result. What is the benefit of using **
as a parameter value?
Upvotes: 3
Views: 6331
Reputation: 63
The benefit of having the double splat operator **
as argument is mainly that you can avoid passing any argument at all. Much like the same of what happens when you use the single splat operator *
.
Using your examples and calling:
test "s"
# raises ArgumentError (wrong number of arguments (given 1, expected 2))
test_two "s"
# works, prints `{} Hash`
This is useful in methods where you want to have a "main" argument, and usually some options, without cluttering the arguments space. For example, imagine a CSV row parser:
def parse_row_1(row, **options)
separator = options.fetch :separator, ","
quote_char = options.fetch :quote_char, null
# ...
end
def parse_row_2(row, separator = ",", quote_char = null)
# ...
end
# To parse a string like this:
s = "'ABC','123','DEF'"
# With ** method you can do just this:
parse_row_1 s, quote_char: "'"
# Without ** you must specify every time the arguments, because they are positional:
parse_row_2 s, ",", "'"
Upvotes: 2
Reputation: 4348
Ruby 2.7 started more clearly differentiating between keyword arguments and regular hashes. **args
is for keyword arguments. Some implications:
def test3(some_string, foo:, **args)
puts args
end
test3('a', foo: 'b', bar: 'c') # => {:bar=>"c"}
works as expected, however
def test3(some_string, foo:, hash)
puts args
end # => syntax error
def test3(some_string, hash, foo:)
puts args
end # works so far
test3('a', foo: 'b', bar: 'c')
# warning: Passing the keyword argument as the last hash parameter is deprecated
# ArgumentError (missing keyword: :foo)
Once you upgrade to ruby 3, the warnings turn to errors.
Upvotes: 4