I have code similar to the snippet below:
···
--
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?
I've tried other variations on self.class.class_eval, including
self.instance_eval, eval("b.call", binding), all to no avail. Any help would
be appreciated.
--
Bill Atkins