Reputation: 1051
I want to pass a symbol into a method and have it
def block
@something = Something.find(1)
hsh = {:type => :method_to_perform}
hsh.each { |k, v| nil_test k, v }
end
def nil_test k, v
@something.try(:k) ? (@k = @something.k.v) : nil
end
I'm running into one or maybe two issues, first I think passing the symbol isn't acting as a method on @something
. How do you pass a symbol and have it act as a method into another block?
Second, I want the key to become the symbol, in others words in the above example I want @k
to be @type
, not @k
. Know how to solve this problem?
Upvotes: 0
Views: 358
Reputation: 96944
You need to use send
(or public_send
):
@something.try(k) ? instance_variable_set(:@k, @something.send(k).send(v)) : nil
You might as well just do
instance_variable_set(:@k, @something.try(k).try(v))
which is equivalent.
Upvotes: 1