Hi,
In message "Re: Strange behavior with constants"
on Wed, 3 Aug 2005 03:41:42 +0900, Bill Atkins <batkins57 / gmail.com> writes:
|class X
|def initialize &b
|self.class.class_eval &b
|end
|end
|
|class Y < X
|def initialize &b
|super(&b)
|end
|end
|
|a = Y.new do
|CONST = 3
|end
|
|puts class Y; CONST; end
|puts class X; CONST; end
|Ideally, calling Y's constructor should create a constant in Y and Y alone,
|since self in X#initialize is an instance of Y. However, running this code
|yields 3 and 3 - somehow the constant has been declared in X instead of in
|Y.
|
|What am I doing wrong?
Constant assignment is done in the context determined at compile time,
that means you have defined CONST in the toplevel. Use const_set
method instead.
matz.