ruby 类变量上的 Attr_accessor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21122691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attr_accessor on class variables
提问by Talespin_Kit
attr_accessordoes not work on the following code. The error says "undefined method 'things' for Parent:Class (NoMethodError)":
attr_accessor不适用于以下代码。错误说“ undefined method 'things' for Parent:Class (NoMethodError)”:
class Parent
@@things = []
attr_accessor :things
end
Parent.things << :car
p Parent.things
However the following code works
但是以下代码有效
class Parent
@@things = []
def self.things
@@things
end
def things
@@things
end
end
Parent.things << :car
p Parent.things
回答by Alex.Bullard
attr_accessordefines accessor methods for an instance. If you want class level auto-generated accessors you could use it on the metaclass
attr_accessor为实例定义访问器方法。如果你想要类级自动生成的访问器,你可以在元类上使用它
class Parent
@things = []
class << self
attr_accessor :things
end
end
Parent.things #=> []
Parent.things << :car
Parent.things #=> [:car]
but note that this creates a class level instance variable nota class variable. This is likely what you want anyway, as class variables behave differently then you might expect when dealing w/ inheritence. See "Class and Instance Variables In Ruby".
但请注意,这会创建一个类级别的实例变量而不是类变量。无论如何,这可能是您想要的,因为类变量的行为与您在处理继承时可能期望的不同。请参阅“ Ruby 中的类和实例变量”。
回答by Chuck
attr_accessorgenerates accessors for instancevariables. Class variables in Ruby are a very different thing, and they are usually not what you want. What you probably want here is a class instance variable. You can use attr_accessorwith class instance variables like so:
attr_accessor为实例变量生成访问器。Ruby 中的类变量是非常不同的东西,它们通常不是您想要的。你可能想要的是一个类实例变量。您可以attr_accessor像这样使用类实例变量:
class Something
class <<self
attr_accessor :things
end
end
Then you can write Something.things = 12and it will work.
然后你可以写Something.things = 12,它会起作用。
回答by Paul Byrne
This is probably the simplest way.
这可能是最简单的方法。
class Parent
def self.things
@@things ||= []
end
end
Parent.things << :car
p Parent.things
回答by Vlad Khomich
Just some clarification: class variables won't be accessible using attr_accessor. It's all about instance variables:
只是一些澄清:类变量将无法使用attr_accessor. 这都是关于实例变量的:
class SomeClass
class << self
attr_accessor :things
end
@things = []
end
because in Ruby, class is an instance of the class "Class" (God, I love to say that) and attr_accessorsets accessor methods for instance variables.
因为在 Ruby 中,class 是类“Class”(上帝,我喜欢这么说)的一个实例,并attr_accessor为实例变量设置访问器方法。
回答by artamonovdev
Аlso note that a singleton methodis a method only for a single object. In Ruby, a Class is also an object, so it too can have singleton methods! So be aware of when you might be calling them.
还请注意,单例方法是仅用于单个对象的方法。在 Ruby 中,一个类也是一个对象,所以它也可以有单例方法!因此,请注意您可能会致电他们的时间。
Example:
例子:
class SomeClass
class << self
def test
end
end
end
test_obj = SomeClass.new
def test_obj.test_2
end
class << test_obj
def test_3
end
end
puts "Singleton methods of SomeClass"
puts SomeClass.singleton_methods
puts '------------------------------------------'
puts "Singleton methods of test_obj"
puts test_obj.singleton_methods
Singleton methods of SomeClass
SomeClass 的单例方法
test
测试
Singleton methods of test_obj
test_obj 的单例方法
test_2
测试_2
test_3
测试_3
回答by su_li
Parent.class_variable_get(:@@things)
That would be the built-in way. In most cases this should be sufficient I think. No need to have a class variable accessor in the instance.
那将是内置的方式。在大多数情况下,我认为这应该足够了。实例中不需要有类变量访问器。

