我正在执行以下操作,期望TestClass.my_var
返回"my_var_here"
:
irb(main):001:0> def create_a_class class_name, my_var
irb(main):002:1> klass = Object.const_set class_name, Class.new
irb(main):003:1> klass.class_variable_set :@@my_var, my_var
irb(main):004:1> klass.instance_eval do
irb(main):005:2* def my_var
irb(main):006:3> @@my_var
irb(main):007:3> end
irb(main):008:2> end
irb(main):009:1> klass
irb(main):010:1> end
=> nil
irb(main):011:0> create_a_class "TestClass", "my_var_here"
=> TestClass
相反,我明白了:
irb(main):012:0> TestClass.my_var
(irb):6: warning: class variable access from toplevel
NameError: uninitialized class variable @@my_var in Object
from (irb):6:in `my_var'
from (irb):12
from C:/Ruby193/bin/irb:12:in `<main>'
我做错了什么?任何意见都将不胜感激。
编辑:我已经尝试过这样做了它似乎有效,但它并没有真正感觉像Ruby Way™这样做(我也不想有那些讨厌的警告)irb(main):001:0> def create_a_class class_name, _my_var
irb(main):002:1> klass = Object.const_set class_name, Class.new
irb(main):003:1> klass.instance_eval do
irb(main):004:2* @@my_var = _my_var
irb(main):005:2> def my_var
irb(main):006:3> @@my_var
irb(main):007:3> end
irb(main):008:2> end
irb(main):009:1> klass
irb(main):010:1> end
=> nil
irb(main):011:0> create_a_class "TestClass", "my_var_here"
(irb):4: warning: class variable access from toplevel
=> TestClass
irb(main):012:0> TestClass.my_var
(irb):6: warning: class variable access from toplevel
=> "my_var_here"
答案 0 :(得分:2)
首先? 为什么您使用的是类变量吗?你知道你也可以在类上使用实例变量吗?它们更容易预测。
类词变量在词汇上被查找:
class Foo
@@a = 1
end
class Bar
@@a = 2
def Foo.a; @@a end
end
p Foo.a # => 2
如果你真的想使用类变量,那么你必须使用#eval(某种形式)来定义方法:
def create_a_class class_name, my_var
klass = Object.const_set class_name, Class.new
klass.class_variable_set :@@my_var, my_var
klass.class_eval <<-RUBY
def self.my_var
@@my_var
end
RUBY
klass
end
create_a_class "Name", "var"
p Name.my_var