I work through Pickaxe 1.9, and I am a little confused by the constant search in instance / class_eval blocks. I am using 1.9.2.
Ruby seems to handle persistent searches in * _eval blocks in the same way as the lookup method:
- find the definition in receiver.singleton_class (plus mixins);
- then to receiver.singleton_class.superclass (plus mixins);
- then continue your own chain until you reach
#<Class:BasicObject>
; - whose superclass is the class;
- and then up the rest of the chain of ancestors (including
Object
, which stores all the constants that you define at the top level), checking for mixes along the way
It is right? The discussion of Pickaxe is a little bit.
Some examples:
class Foo CONST = 'Foo::CONST' class << self CONST = 'EigenFoo::CONST' end end Foo.instance_eval { CONST }
In the class_eval example, Foo-the-class does not stop along the chain of ancestors Foo-the-object!
And an example with mixins:
module M CONST = "M::CONST" end module N CONST = "N::CONST" end class A include M extend N end A.instance_eval { CONST }
source share