Help - calling methods in a hash with parameters

Hiya,

If I want to create a hash table with the values containing methods to
call, how do i call these methods if they need parameters? Here’s an
example:

irb(main):001:0> def methodone
irb(main):002:1> puts "hi, i am method one"
irb(main):003:1> end
=> nil
irb(main):004:0> def methodtwo(input)
irb(main):005:1> puts input
irb(main):006:1> end
=> nil
irb(main):007:0> myhash = { ‘foo’ => ‘methodone’, ‘bar’ => ‘methodtwo’ }
=> {“foo”=>“methodone”, “bar”=>“methodtwo”}
irb(main):008:0> eval myhash[“foo”]
hi, i am method one
=> nil
irb(main):009:0> eval myhash[“bar”] "hi, i am method two"
SyntaxError: compile error
(irb):9: parse error
from (irb):9
irb(main):010:0>

In other words, how do I pass a parameter to method two?

If my implementation of what I want to do looks lame, please let me know
of a better way… I am pretty new to Ruby.

Thanks!
Tuan

eval myhash[“bar”] + ‘(“Thing to say”)’

···

On Monday, Jul 14, 2003, at 14:31 America/Chicago, Tuan Bui wrote:

irb(main):001:0> def methodone
irb(main):002:1> puts “hi, i am method one”
irb(main):003:1> end
=> nil
irb(main):004:0> def methodtwo(input)
irb(main):005:1> puts input
irb(main):006:1> end
=> nil
irb(main):007:0> myhash = { ‘foo’ => ‘methodone’, ‘bar’ => ‘methodtwo’
}
=> {“foo”=>“methodone”, “bar”=>“methodtwo”}
irb(main):008:0> eval myhash[“foo”]
hi, i am method one
=> nil
irb(main):009:0> eval myhash[“bar”] “hi, i am method two”
SyntaxError: compile error
(irb):9: parse error
from (irb):9
irb(main):010:0>

In other words, how do I pass a parameter to method two?


John Platte
Principal Consultant, NIKA Consulting
http://nikaconsulting.com/

Tuan Bui wrote:

If I want to create a hash table with the values containing methods to
call, how do i call these methods if they need parameters?

Here’s one alternative:

 myhash = { 'foo' => method('methodone'),
            'bar' => method('methodtwo') }

 myhash["foo"].call
 myhash["bar"].call "hi, i am method two"

The method() method returns a reference to a Method object for the named
method (could I use the word “method” a few more times in that
sentence?) And call() is an instance method for Method objects.

Hope this helps,

Lyle

In case anyone wants to know of a solution, Lyle was kind enough to
email me one:

Lyle Johnson wrote:

Tuan Bui wrote:

If I want to create a hash table with the values containing methods
to call, how do i call these methods if they need parameters?

Here’s one alternative:

myhash = { 'foo' => method('methodone'),
           'bar' => method('methodtwo') }

myhash["foo"].call
myhash["bar"].call "hi, i am method two"

The method() method returns a reference to a Method object for the
named method (could I use the word “method” a few more times in that
sentence?) And call() is an instance method for Method objects.

···

Hope this helps,

Lyle

An alternative to the possibilities already mentioned would be to just
send (or send if necesarry), the method name + parameters to the
receiver:

send myhash[‘bar’], “hello world” # outputs “hello world”

···

On 2003-07-15 04:31:54 +0900, Tuan Bui wrote:

In other words, how do I pass a parameter to method two?


Computer programming is an art, because it applies accumulated knowledge
to the world, because it requires skill and ingenuity, and especially
because it produces objects of beauty. Programmers who subconsciously
view themselves as artists will enjoy what they do and will do it better.
– Donald Knuth, Computer Programming as an Art. Turing Award Speech

eval myhash[“bar”] + ‘(“Thing to say”)’

No, don’t do that! Calling the compiler should be a last resort.

Instead, convert your method into an object reference so it can be put
straight into the hash:

myhash = {‘foo’ => method(:methodone), ‘bar’ => method(:methodtwo)}

myhash[‘foo’].call
myhash[‘bar’].call(‘hi, I am method two’)

Check the docs for Method to find out other useful things (e.g. Method#arity
will tell you how many arguments it needs, before you call it)

Regards,

Brian.

···

On Tue, Jul 15, 2003 at 04:47:55AM +0900, John Platte wrote:

On Monday, Jul 14, 2003, at 14:31 America/Chicago, Tuan Bui wrote:

irb(main):001:0> def methodone
irb(main):002:1> puts “hi, i am method one”
irb(main):003:1> end
=> nil
irb(main):004:0> def methodtwo(input)
irb(main):005:1> puts input
irb(main):006:1> end
=> nil
irb(main):007:0> myhash = { ‘foo’ => ‘methodone’, ‘bar’ => ‘methodtwo’
}
=> {“foo”=>“methodone”, “bar”=>“methodtwo”}
irb(main):008:0> eval myhash[“foo”]
hi, i am method one
=> nil
irb(main):009:0> eval myhash[“bar”] “hi, i am method two”
SyntaxError: compile error
(irb):9: parse error
from (irb):9
irb(main):010:0>

In other words, how do I pass a parameter to method two?


John Platte
Principal Consultant, NIKA Consulting
http://nikaconsulting.com/