How can I prevent a positional argument from being expanded into keyword arguments?

arguments, keyword-argument, ruby, ruby-2.0

Solution

This is a bug that was fixed in Ruby 2.0.0-p247, see this issue.

Problem

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?

Original source