From: Bruno Michel Date: 2006-10-02T22:42:19+09:00 Subject: Re: Creating dynamically named singleton methods. Syntax question. Martin Coxall a �crit : > On 10/2/06, Jan Svitok wrote: >> >> On 10/2/06, Luke Stark 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 Hi, You can use class_eval, or better no *eval function : $ cat my_thing.rb #!/usr/bin/env ruby class MyThing; end foo = MyThing.new class << foo bar = "do_stuff" define_method(bar) { "code" } end puts foo.do_stuff $ ruby my_thing.rb code -- Bruno Michel