Class eval does not work with self

A

Andy Frank

I am experimenting with a test class to figure out why self.class_eval
does not seem to work. Please see code below:

class Xclass
def add_method(meth, work)
self.class_eval %Q{
def #{meth}
#{work}
end
}
end
end

This throws an error when I call the class like so:

x = Xclass.new
x.add_method("add", "puts \"Hello World\"")


Error:
rb:3:in `add_method': undefined method `class_eval' for #
(NoMethodError)
from test_dyn.rb:15:in `'


The fix has been to change self.class_eval to Xclass.class_eval.
Anyone know why this is the case. Shouldn't self work.
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

No, self is not Xclass inside of add_method. It's the instance of Xclass. If
you made add_method a class method (def self.add_method...), then it should
work as written.
 
J

Jesús Gabriel y Galán

I am experimenting with a test class to figure out why self.class_eval
does not seem to work. =A0Please see code below:

=A0class Xclass
=A0 =A0 =A0 =A0 def add_method(meth, work)
=A0 =A0 =A0 =A0 =A0self.class_eval %Q{
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 def #{meth}
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #{work}
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 end
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
=A0 =A0 =A0 =A0 end
end

This throws an error when I call the class like so:

x =3D Xclass.new
x.add_method("add", "puts \"Hello World\"")


Error:
rb:3:in `add_method': undefined method `class_eval' for #
(NoMethodError)
=A0from test_dyn.rb:15:in `'


The fix has been to change self.class_eval =A0to Xclass.class_eval.
Anyone know why this is the case. Shouldn't self work.

You can also use self.class.class_eval:

irb(main):001:0> class Xclass
irb(main):002:1> def add_method(meth,work)
irb(main):003:2> self.class.class_eval %Q{
irb(main):004:2" def #{meth}
irb(main):005:2" #{work}
irb(main):006:2" end
irb(main):007:2" }
irb(main):008:2> end
irb(main):009:1> end
=3D> nil
irb(main):010:0> x =3D Xclass.new
=3D> #<Xclass:0xb74f77b8>
irb(main):011:0> x.add_method("add", 'puts "hello world"')
=3D> nil
irb(main):012:0> x.add
hello world

Jesus.
 
J

Jeremy Bopp

You can also use self.class.class_eval:

irb(main):001:0> class Xclass
irb(main):002:1> def add_method(meth,work)
irb(main):003:2> self.class.class_eval %Q{
irb(main):004:2" def #{meth}
irb(main):005:2" #{work}
irb(main):006:2" end
irb(main):007:2" }
irb(main):008:2> end
irb(main):009:1> end
=> nil
irb(main):010:0> x = Xclass.new
=> #<Xclass:0xb74f77b8>
irb(main):011:0> x.add_method("add", 'puts "hello world"')
=> nil
irb(main):012:0> x.add
hello world

Or you can just use eval instead of class_eval in this case:

irb(main):001:0> class Xclass
irb(main):002:1> def add_method(meth, work)
irb(main):003:2> eval %Q{
irb(main):004:2" def #{meth}
irb(main):005:2" #{work}
irb(main):006:2" end
irb(main):007:2" }
irb(main):008:2> end
irb(main):009:1> end
=> nil
irb(main):010:0> x = Xclass.new
=> #<Xclass:0x7ff80244>
irb(main):011:0> x.add_method("add", 'puts "hello world"')
=> nil
irb(main):012:0> x.add
hello world

-Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

class_eval 4
require does not work 3
self in irb versus script 3
class << self 12
self 16
local variable and closure 3
Eval needs explicit self for accessor methods? 7
better error reporting from eval? 2

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top