我想知道我怎么可以访问模块如何从模块访问类变量?
module Entity
def foo
# puts @@rules
end
end
class Person
include Entity
attr_accessor :id, :name
@@rules = [[:id, :int, :not_null],
[:name, :string, :not_null]]
end
class Car
include Entity
attr_accessor :id, :year
@@rules = [[:id, :string, :not_null],
[:year:, :int, :not_null]]
end
p = Person.new
c = Car.new
p.foo # [[:id, :int, :not_null], [:name, :string, :not_null]]
c.foo # [[:id, :string, :not_null], [:year, :int, :not_null]]
我在cattr_accessor
看了看,mattr_accessor
类变量从ActiveSupport
,但仍然无法找到一个方法来解决这个问题。
好吧,ty!代码的作品,这很好!我希望能得到一个代码更加优雅的答案! – Quarktum