attr_accessor
在以下代码中无法工作。错误提示为 "undefined method 'things' for Parent:Class (NoMethodError)
":
class Parent
@@things = []
attr_accessor :things
end
Parent.things << :car
p Parent.things
然而下面的代码可以正常工作
class Parent
@@things = []
def self.things
@@things
end
def things
@@things
end
end
Parent.things << :car
p Parent.things
require 'active_support'
,你可以使用cattr_accessor :things。 - Cary Swoveland