From: Brian Candler Date: 2003-07-15T04:58:31+09:00 Subject: Re: help - calling methods in a hash with parameters On Tue, Jul 15, 2003 at 04:47:55AM +0900, John Platte wrote: > 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 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/ > >