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.
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.
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