You can destructure an array by using the splat operator.
def foo(arg1, arg2, arg3)
#...Do Stuff...
end
array = ['arg2', 'arg3']
foo('arg1', *array)
But is there a way to destruct a hash for option type goodness?
def foo(arg1, opts)
#...Do Stuff with an opts hash...
end
opts = {hash2: 'bar', hash3: 'baz'}
foo('arg1', hash1: 'foo', *opts)
If not native ruby, has Rails added something like this?
Currently I'm doing roughly this with
foo('arg1', opts.merge(hash1: 'foo'))
Yes, there is a way to de-structure a hash:
def f *args; args; end
opts = {hash2: 'bar', hash3: 'baz'}
f *opts #=> [[:hash2, "bar"], [:hash3, "baz"]]
The problem is that you what you want is actually not de-structuring at all. You’re trying to go from
'arg1', { hash2: 'bar', hash3: 'baz' }, { hash1: 'foo' }
(remember that 'arg1', foo: 'bar'
is just shorthand for 'arg1', { foo: 'bar' }
) to
'arg1', { hash1: 'foo', hash2: 'bar', hash3: 'baz' }
which is, by definition, merging (note how the surrounding structure—the hash—is still there). Whereas de-structuring goes from
'arg1', [1, 2, 3]
to
'arg1', 1, 2, 3
It's 2018 and this deserves an update. Ruby 2.0 introduced keyword arguments and with that also the hash splat operator **
. Now you can simply do the following:
def foo(arg1, opts)
[arg1, opts]
end
opts = {hash2: 'bar', hash3: 'baz'}
foo('arg1', hash1: 'foo', **opts)
#=> ["arg1", {:hash1=>"foo", :hash2=>"bar", :hash3=>"baz"}]
There is no such thing (although it has been proposed). Since this will change the parsing rule, it cannot be implemented within Ruby. The best I can think of is to define *
on hash like
class Hash; alias :* :merge end
and use it in one of the following ways:
foo('arg1', {hash1: 'foo'}*opts)
foo('arg1', {hash1: 'foo'} *opts)
foo('arg1', {hash1: 'foo'}. *opts)
the last of which I think is reasonably close to what you wanted.
If you're ok with using active_support:
require 'active_support/core_ext/hash/slice.rb'
def foo(*args)
puts "ARGS: #{args}"
end
opts = {hash2: 'bar', hash3: 'baz'}
foo *opts.slice(:hash2, :hash3).values
...or you could monkey-patch your own solution:
class Hash
def pluck(*keys)
keys.map {|k| self[k] }
end
end
def foo(*args)
puts "ARGS: #{args}"
end
opts = {hash2: 'bar', hash3: 'baz'}
foo *opts.pluck(:hash2, :hash3)
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