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?
Ruby 2.7 started more clearly differentiating between keyword arguments and regular hashes.
**args
is for keyword arguments. Some implications:works as expected, however
Once you upgrade to ruby 3, the warnings turn to errors.