D
DMG
Can anybody explain me, why this is true:
self.class.class_eval{self} == self.class.instance_eval{self}
Thanks,
Dawid
self.class.class_eval{self} == self.class.instance_eval{self}
Thanks,
Dawid
Can anybody explain me, why this is true:
self.class.class_eval{self} == self.class.instance_eval{self}
Thanks,
Dawid
unknown said:It seems that we don't need class_eval method. Because if we use a
Module or Class directly, we can use:
SomeClass.instance_eval { ... }
and if we have an object and we need to work on its class, we can use:
some_object.class.instance_eval { ... }
So, do we need class_eval/module_eval?
When Ruby code is executing, there are two bits of state which are
maintained:
- the current object, which is exposed as 'self'
- the current class, where instance methods are defined. This is pretty
well hidden, but sometimes it matters.
Try the following code:
class Foo; end
Foo.class_eval "def bar; puts 'Hello!'; end"
Foo.new.bar
If you change class_eval to instance_eval, it won't work as shown. In
fact you'll make a class method on Foo (Foo.bar)
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.