I have a method that looks like this:
def method(:name => nil, :color => nil, shoe_size => nil)
SomeOtherObject.some_other_method(THE HASH THAT THOSE KEYWORD ARGUMENTS WOULD MAKE)
end
For any given call, I can accept any combination of optional values. I like the named arguments, because I can just look at the method's signature to see what options are available.
What I don't know is if there is a shortcut for what I have described in capital letters in the code sample above.
Back in the olden days, it used to be:
def method(opts)
SomeOtherObject.some_other_method(opts)
end
Elegant, simple, almost cheating.
Is there a shortcut for those Keyword Arguments or do I have to reconstitute my options hash in the method call?
Of course! Just use the double splat (
**
) operator.It works just like the regular splat (
*
), used for collecting parameters. You can even forward the keyword arguments to another method.Yes, this is possible, but it's not very elegant.
You'll have to use the
parameters
method, which returns an array of the method's parameters and their types (in this case we only have keyword arguments).Knowing that, there's various ways how to use that array to get a hash of all the parameters and their provided values.
So your example would look like
Think carefully about using this. It's clever but at the cost of readability, others reading your code won't like it.
You can make it slightly more readable with a helper method.
Update: Ruby 2.2 introduced
Binding#local_variables
which can be used instead ofMethod#parameters
. Be careful because you have to calllocal_variables
before defining any additional local variables inside the method.I had some fun with this, so thanks for that. Here's what I came up with:
I chose to return an array, but you could easily modify this to return a hash instead (for instance, by not caring about the argument type after the initial detection).
How about the syntax below?
For it to work, treat
params
as a reserved keyword in your method and place this line at the top of the method.