From: Martin Coxall Date: 2006-10-02T22:19:04+09:00 Subject: Re: Creating dynamically named singleton methods. Syntax question. ------=_Part_6873_18532721.1159795141679 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 10/2/06, Martin Coxall wrote: > > > > On 10/2/06, Luke Stark wrote: > > > > You may create singleton methods like so: > > > > foo = MyThing.new > > > > def foo.do_stuff > > "code" > > end > > > > But I cannot seem to grock the syntax that would allow me to name the > > method from a variable. Such as: > > > > foo = MyThing.new > > bar = "do_stuff" > > def foo.%{#{bar}} #This is totally wrong I think. > > "code" > > end > > > > Any suggestions? I'm starting to get a spinning head. :) > > > > To be clear, I want to create a singleton method on the instance of > > MyThing, not add the method to the MyThing class. > > > > This is mentioned here: > > > > http://www.rubyist.net/~slagell/ruby/singletonmethods.html > > > > but I'd like to do it dynamically. > > > > Something like this: > > obj.instance_eval do > define method(:methodName) do |*args| > # some shit here > end > end > And, of course, that should be define_method(). Martin ------=_Part_6873_18532721.1159795141679--