From: Devin Mullins Date: 2005-08-10T19:55:05+09:00 Subject: Re: passing a method Wybo Dekker wrote: > I want to make a method which can execute another method given to it as > an argument. The following works but the call doesn't look elegant - is > it the ruby way? Since all your methods are on the same object, there's a simpler way: C:\>ruby @t = '' def bye @t << 'bye' end def hello @t << 'hello' end def doit(f) send f #ri Object#send end doit :hello doit :bye doit :hello puts @t ^D hellobyebello C:\> You could also try passing blocks: C:\>ruby @t = '' def bye @t << 'bye' end def hello @t << 'hello' end def doit yield end doit { hello } doit { bye } doit { hello } puts @t ^D hellobyebello C:\> Or modules/classes: C:\>ruby module Bye def self.greet(t) t << 'bye' end end class Hello def self.greet(t) t << 'hello' end end @t = '' def doit(c) c.greet @t end doit Hello doit Bye doit Hello puts @t ^D hellobyehello C:\> Longer, but maybe more elegant? Meh, I like the first one best. Devin