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 <<s
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
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?
Thanks!
···
from :0
----------------------------
Eustáquio "TaQ" Rangel
eustaquiorangel@yahoo.com
http://beam.to/taq
Usuário GNU/Linux no. 224050