From: "David A. Black" Date: 2005-08-10T20:28:12+09:00 Subject: Re: passing a method Hi -- On Wed, 10 Aug 2005, 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? > > wybo>cat test.rb > #!/usr/bin/env ruby > @t = '' > def bye > @t << 'bye' > end > def hello > @t << 'hello' > end > def doit(f) > f.call > end > doit(method(:hello)) > doit(method(:bye)) > doit(method(:hello)) > puts @t > wybo>ruby test.rb > hellobyehello > wybo> Have you looked into the 'send' method? str = "a string" array = str.send(:split, //) p array # => ["a", " ", "s", "t", "r", "i", "n", "g"] The first argument is the method name; args 2+ are the args to the method. You can use strings as well as symbols for the method name. David -- David A. Black dblack@wobblini.net