From: Jean Helou Date: 2006-10-02T22:32:03+09:00 Subject: Re: Creating dynamically named singleton methods. Syntax question. class MyThing; end f=MyThing.new f.instance_eval do class << self define_method :test {puts "test"} end end jean 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 > > Regards, > > Martin > >