Rails 中 mattr_accessor 一处文档错误

时间:2021-05-14 21:00:07

http://xiewenwei.github.io/blog/2015/01/11/mattr-accessor-in-ruby-on-rails-activesupport/

 module HairColors
extend ActiveSupport::Concern def self.included(b)
b.class_eval do
mattr_accessor :hair_colors
end
self.class_eval do
@@hair_color = nil def self.hair_colors
@@hair_color
end def self.hair_colors=(obj)
@@hair_color = obj
end
end
end
end class Person
include HairColors
end Person.hair_colors = [:brown, :black, :blonde, :red]
p Person.hair_colors
p Person.new.hair_colors
HairColors.hair_colors=23
p HairColors.hair_colors p Person.hair_colors
p Person.new.hair_colors