S
Steve Hull
Hey ruby wizards. I got a question for you. I want to call a method on
my object instance, with the method name specified by a string. I want
to *pass in* a hash to my method.
I tried:
myMethodName = "doSomeStuff"
myObject.instance_eval "#{myMethodName}(#{myHash})"
But it complained that it didn't have a method named #{myHash.to_s}.
So I tried myObject.instance_eval { myMethodName(myHash) }
But it compalined that it didn't have a method named "myMethodName"
(which is obvious -- the method I wanted to call is doSomeStuff).
I'm new to metaprogramming in ruby. What am I missing here?
Last thought: I guess I could assign a transient local var
"@input_hash", then call the method which would use @input_hash instead
of requiring an input. That just seems a little ugly though. Any
prettier solution?
-Steve
my object instance, with the method name specified by a string. I want
to *pass in* a hash to my method.
I tried:
myMethodName = "doSomeStuff"
myObject.instance_eval "#{myMethodName}(#{myHash})"
But it complained that it didn't have a method named #{myHash.to_s}.
So I tried myObject.instance_eval { myMethodName(myHash) }
But it compalined that it didn't have a method named "myMethodName"
(which is obvious -- the method I wanted to call is doSomeStuff).
I'm new to metaprogramming in ruby. What am I missing here?
Last thought: I guess I could assign a transient local var
"@input_hash", then call the method which would use @input_hash instead
of requiring an input. That just seems a little ugly though. Any
prettier solution?
-Steve