M
mzhang.cn
Hi all,
Please look at the following code
-----------------------------------------------------------
class Cls
@@cv = 0
def initialize
@iv = 1
end
end
c=Cls.new
Cls.class_eval { def iv() @iv end }
puts c.iv
Cls.class_eval { def cv() @@cv end }
puts c.cv
-------------------------------------------------------------
When running this code snippet, I got the result:
$ ruby classvar.rb
1
classvar.rb:11:in `cv': uninitialized class variable @@cv in Object
(NameError).
Why I can't get the class variable in class_eval?
Please look at the following code
-----------------------------------------------------------
class Cls
@@cv = 0
def initialize
@iv = 1
end
end
c=Cls.new
Cls.class_eval { def iv() @iv end }
puts c.iv
Cls.class_eval { def cv() @@cv end }
puts c.cv
-------------------------------------------------------------
When running this code snippet, I got the result:
$ ruby classvar.rb
1
classvar.rb:11:in `cv': uninitialized class variable @@cv in Object
(NameError).
Why I can't get the class variable in class_eval?