NullVoxPopuli
NullVoxPopuli

Reputation: 65183

How do you convert a hash of things to params to be sent to a method in ruby?

I'm trying to call a method that takes n number of arguments, and the arguments are passed in as a hash to the method that calls the method with n arguments, but a hash just looks like another paramater. How do I unhash my parameter so the method gets called correctly?

Some code, cause this is hard to explain with just words:

the method I want to call: method(p[:method]).call(p[:action], p[:params])
p[:method] in this case is :post
p[:action] is :create
p[:params] is {:my_object => {my object's params}}

so, unravelled, it looks like this:
post(:create, {:my_object => {my object's params}}) # current
but what it should look like, is this:
post(:create, :my_object => {my object's params}) # desired


How do I change method(p[:method]).call(p[:action], p[:params]) such that I can get the desired method call?

Upvotes: 0

Views: 176

Answers (1)

fl00r
fl00r

Reputation: 83680

Actually

post(:create, {:my_object => {my object's params}})

and

post(:create, :my_object => {my object's params})

are the same constructions.

In both cases you will path two attributes to method post: symbol :create and a hash.

Anyway, you can do this ugly trick, in case if you have got only one key in params:

method(p[:method]).call(p[:action], p[:params].keys.first => p[:params].values.first)

Upvotes: 1

Related Questions