From: Robert Klemme Date: 2006-03-26T07:53:54+09:00 Subject: Re: Object methods Eustaquio Rangel de Oliveira J wrote: > Hello there. :-) > > Ok, I know I can do this to add a method (and a class) > to an object: > > irb(main):001:0> s = "Testing!" > => "Testing!" > irb(main):002:0> class < irb(main):003:1> def says > irb(main):004:2> puts "s says:"+self > irb(main):005:2> end > irb(main):006:1> end > => nil > irb(main):007:0> s.says > s says:Testing! > => nil > irb(main):008:0> x = "Another test." > => "Another test." > irb(main):009:0> x.says > NoMethodError: undefined method `says' for "Another > test.":String > from (irb):9 > from :0 > irb(main):010:0> > > My question is, dinamically, how to do that? Just like > this: > > irb(main):010:0> x.class.class_eval %q(def x.says() > puts "x says:"+self end) > => nil > irb(main):011:0> x.says > x says:Another test. > > Or there is another shorter way to do that, with an > object? To build an object method with some parameters > like a string or a Proc? I'm not sure what exactly you're after. Do you mean class String def says print "s says: ", self, "\n" end end ? robert