this seems to work $ irb irb(main):001:0> o=Object.new => #<Object:0x100e3128> irb(main):002:0> s="test" => "test" irb(main):003:0> sym=s.to_sym => :test irb(main):004:0> o.instance_eval do irb(main):005:1* puts s irb(main):006:1> klass=class<<self;self;end irb(main):007:1> klass.send(:define_method,sym){puts "#{s}"} irb(main):008:1> end test => #<Proc:0x003df7d8@(irb):7> irb(main):009:0> o.test test => nil On 10/2/06, Luke Stark <Luke.Stark / mathworks.com> wrote: > Jean, > > Hmm. This looks close, but the :test in your example can only be a > variable that has been defined within the instance_eval...and I need to > pass one from outside. > > The others run, but add the method to the class, rather than the > instance of that class. > > I'm sure there's a simple way. I just can't see it. > > -L > > > -----Original Message----- > > From: Jean Helou [mailto:jean.helou / gmail.com] > > Sent: Monday, October 02, 2006 9:41 AM > > To: ruby-talk ML > > Subject: Re: Creating dynamically named singleton methods. Syntax > > question. > > > > However this will define the given method for all new instances of > > obj.class (MyThing in my example) which are created after this code is > > executed, which is not what the OP seemed to want. > > > > jean > > > > ps: in my example :test can be a variable and String.to_sym is your > > friend. > > > > On 10/2/06, Martin Coxall <pseudo.meta / gmail.com> wrote: > > > On 10/2/06, Jan Svitok <jan.svitok / gmail.com> wrote: > > > > > > > > On 10/2/06, Luke Stark <Luke.Stark / mathworks.com> wrote: > > > > > You may create singleton methods like so: > > > > > > > > > > > > > > I'll have one more go. This time I'll get it right. ;p > > > > > > obj.class.instance_eval do > > > define_method(:method_name) do |*args| > > > # Method goes here > > > end > > > end > > > > > > N.B. this actually works this time, because I tried it. > > > > > > Regards, > > > > > > Martin > > > > > > > > >