当 Class attribute 遇见 inheritance

Ruby的类变量遇到继承的时候:

  class F
@@a = 'f'
def foo
puts @@a
end
end

class A < F
@@a = 'a'
end

class B < F
@@a = 'b'
end


a = A.new
a.foo # => 'b'


Rails是这么解决的:[url]http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb?rev=7220[/url]

你可能感兴趣的:(当 Class attribute 遇见 inheritance)