user427165
user427165

Reputation:

convert a list of hashes to a argument for a method

I have hash based arguments.

method1(:test=>[:arg1, :arg2 => :something])

I need to pass :test as argument to another method in the following format

from A:

[:arg1, {:arg2=>:something}] 

to B:

method2 :arg1, :arg2=>:something

How can I get from A to B?

Upvotes: 1

Views: 1082

Answers (3)

user483040
user483040

Reputation:

if you don't have a lot of things in your hash, you could just loop on the keys and dereference them. What has to happen for this to work is:

  1. you have to add the key/values in the order you want them.
  2. be using 1.9 (I believe it was in 1.9 that they made it so that key ordering is retained in hashes)

Upvotes: 0

Miki
Miki

Reputation: 7188

If ary = [:art1, {:arg2 => :something}] then method2 *ary should do the trick.

Upvotes: 0

Linus Oleander
Linus Oleander

Reputation: 18157

How about?

args = {:test => [:arg1, :arg2 => :something]}
method1(args)

method2(*args[:test])

Upvotes: 7

Related Questions