From: Mark Volkmann Date: 2002-10-02T12:27:07+09:00 Subject: Re: singleton methods with variable method names ----- Original Message ----- From: "Mark Volkmann" To: "ruby-talk ML" Sent: Tuesday, October 01, 2002 7:10 PM Subject: singleton methods with variable method names > I know I can do this. > > obj = Object.new > def obj.test > puts "running the singleton test method" > end > obj.test # executes the singleton method > > But what if the name of the method I want to add (such as "test") isn't > determined until runtime? > Can I dynamically add a method like that? I think I found the answer myself. Is there a better way than this? obj = Object.new methodName = 'foo' methodDef = "def obj.#{methodName}; puts 'in my method'; end" eval(methodDef) # to add the method foo to the object obj obj.foo # to invoke it