I'd like to have a method that accepts a hash and an optional keyword argument. I tried defining a method like this:
def foo_of_thing_plus_amount(thing, amount: 10)
thing[:foo] + amount
end
When I invoke this method with the keyword argument, it works as I expect:
my_thing = {foo: 1, bar: 2}
foo_of_thing_plus_amount(my_thing, amount: 20) # => 21
When I leave out the keyword argument, however, the hash gets eaten:
foo_of_thing_plus_amount(my_thing) # => ArgumentError: unknown keywords: foo, bar
How can I prevent this from happening? Is there such a thing as an anti-splat?
Using Positional Arguments Followed by Keyword Arguments One method is to simply do what the error states and specify all positional arguments before our keyword arguments! Here, we use the positional argument 1 followed by the keyword argument num2=2 which fixes the error.
Keyword arguments are passed to functions after any required positional arguments. But the order of one keyword argument compared to another keyword argument does not matter.
Unless otherwise specified, an argument can be both positional and keyword. Positional arguments, when provided, must be in sequence. Positional arguments must be used before keyword arguments.
The error positional argument follows keyword argument means that the if any keyword argument is used in the function call then it should always be followed by keyword arguments. Positional arguments can be written in the beginning before any keyword argument is passed.
This is a bug that was fixed in Ruby 2.0.0-p247, see this issue.
What about
def foo_of_thing_plus_amount(thing, opt={amount: 10})
thing[:foo] + opt[:amount]
end
my_thing = {foo: 1, bar: 2} # {:foo=>1, :bar=>2}
foo_of_thing_plus_amount(my_thing, amount: 20) # 21
foo_of_thing_plus_amount(my_thing) # 11
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With