E
Eustaquio Rangel de Oliveira J
Hello there.
Ok, I know I can do this to add a method (and a class)
to an object:
irb(main):001:0> s =3D "Testing!"
=3D> "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
=3D> nil
irb(main):007:0> s.says
s says:Testing!
=3D> nil
irb(main):008:0> x =3D "Another test."
=3D> "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>=20
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)
=3D> 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!
Ok, I know I can do this to add a method (and a class)
to an object:
irb(main):001:0> s =3D "Testing!"
=3D> "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
=3D> nil
irb(main):007:0> s.says
s says:Testing!
=3D> nil
irb(main):008:0> x =3D "Another test."
=3D> "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>=20
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)
=3D> 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!