I have a method defined like this:
def woot(a = nil, b: nil)
...
end
What is the least ugly way to pass a Hash
instance to a
when b
is omitted?
I tried
woot(x: 1)
woot({x: 1})
h = {x: 1}
woot(h)
but they all raise
ArgumentError: unknown keyword: x
The only way to make the call that I have figured out is
woot({x: 1}, **{})
Is there a nicer way?
Your first argument is optional, so Ruby assumes that the hash you're passing is for the named arguments (*).
I don't think there's a better workaround, though this is a bit shorter (but not necessarily cleaner):
woot({x: 1}, {})
If I were you I would change the method signature to resolve the ambiguity.
(*): Well, not always: woot({1=>3})
will happily pass the hash to the first optional argument.
Even weirder, in case of woot({1=> 5, :b => 4})
, Ruby will split the dictionary, and pass {1=>5}
to the first optional parameter.
Looks like it tries to grab the named parameters first (and will complain about not existing names), then pass the rest to the optional parameters.
But I would love to see some language-lawyer explanation...
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