Question

I have a ruby object, say... myobject and a hash, myhash with value { op1: 'something', op2: 'something2' }.

Is there a method in ruby that let me do for example,

myobject.the_method(myhash)

So that it will work as if I did,

myobject.op1 'something'
myobject.op2 'something2'

This can be done with myhash.each and myobject.send , but is there a single method that does this? Active Support solution is acceptable.

Was it helpful?

Solution

There is not a method in Active Support that will do this. I recommend each and send:

hash.each do |method, arg|
  obj.public_send(method, arg)
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top