Eli Bendersky
3/11/2006 9:46:00 AM
Jules Jacobs wrote:
> You can use merge:
>
> def a_method(*options)
> options = {:size => 45, :length => 12}.merge(options)
>
> # do stuff
> end
>
> a_method(:size => 44)
>
> *should* work.
>
This works when I remove the asterisk from the (*options).
Makes sense, I guess, since when I call:
a_method(:size => 21, :weight => 22)
With the asterisk specified, the arguments are folded into an array,
and merge can't take an array as an argument when called on a Hash.
Without the asterisk, Ruby's "special features" takes the arguments I
passed to a_method as a Hash, since nothing came after the last x => y
assignment, and then merge works.
Did I decipher this correctly ?
In any case, your proposal (without the asterisk) works nicely. In the
1st edition of "Programming Ruby" the authors noted that by Ruby 1.8,
named arguments will be built into the language. I assume this hasn't
happened yet ?
Thanks
Eli