From: "Søren Andersen" Date: 2007-10-27T06:00:23+09:00 Subject: Re: Passing methods as parameters - the nice way? ------=_Part_6367_5929357.1193432423981 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 10/24/07, Stefan Rusterholz wrote: > > S=F8ren Andersen wrote: > > Hello all, > > > > I told it to: > > def call_action > > if(@action_method) > > send(@action_method) > > end > > end > > > > which works fine, as long as my method is defined "in the wild": > > > > def foo > > puts "foo called" > > end > > > > interface.set_action(:foo) > > interface.call_action =3D> foo called > > I'd suggest using blocks instead of the above construct. Your code > transformed: > > def on_action(&block) > @action =3D block > end > > def action > @action.call if @action > end > > obj =3D Object.new > def obj.foo; puts "foo on #{self.inspect} called"; end > > interface.on_action { obj.foo } > interface.action > > # alternatively: > interface.on_action(&obj.method(:foo)) > interface.action > > Blocks are far more flexible than passing a method name or instance. > > Regards > Stefan Thanks! I'll try this approach as well! Regards, S=F8ren ------=_Part_6367_5929357.1193432423981--