From: Robert Klemme Date: 2004-09-09T00:30:07+09:00 Subject: Re: method by string ? "Roeland Moors" schrieb im Newsbeitrag news:20040908150955.GA6939@laptop... > On Thu, Sep 09, 2004 at 12:02:24AM +0900, Roeland Moors wrote: > > I'm creating a program which uses a ruby source file as config > > file. > > > > I don't know which methods are defined in this config file. > > > > The problem I have is that I would like to run a method with the same > > name as a string. > > > > Example: > > This is the config file > > > > class Main > > def test1 > > puts 'test1' > > end > > > > def test2 > > puts 'test2' > > end > > end > > > > > > This is the main source: > > > > $method = "test1" > > $mode = Main.new > > > > # this ofcours doesn't work: > > $mode.$method > > > > How should I do this? > > > > Stupid me: > > $mode.method($method) No, that just returns the method object. You then still need to invoke it. >> "foo".method("concat") => # >> "foo".method("concat").call("xxx") => "fooxxx" But #send() is much simpler. Btw, you know that you can do testMethods = $mode.methods.select {|m| /^test/ =~ m} or even $mode.methods.select {|m| /^test/ =~ m}.each do |m| $mode.send m end or $mode.methods.each do |m| $mode.send( m ) if /^test/ =~ m end But it's even more simple to use TestUnit for this... Kind regards robert > > > > -- > > Roeland > > > > > >