Reputation: 1627
I'll try to explain myself a little bit more
# Lets say I have this hash
options = {a: 1, b: 2}
# and here I'm calling the method
some_method(options)
def some_method(options)
# now instead of using options[:a] I'd like to simply use a.
options.delete_nesting_and_create_vars
a + b # :a + :b also good.
thanks!
Upvotes: 1
Views: 44
Reputation: 3984
If your options are fixed, like only :a
and :b
are the only keys, you can write the method like this:
def some_method(a:, b:)
a + b
end
options = {a: 1, b: 2}
some_method(options) #=> 3
Upvotes: 2
Reputation: 121010
Is it possible using Ruby2 splat parameters:
options = {a: 1, b: 2}
def some_method1(a:, b:)
a + b
end
or:
def some_method2(**options)
options[:a] + options[:b]
end
some_method1 **options
#⇒ 3
some_method2 **options
#⇒ 3
Upvotes: 4