Rails 中 mattr_accessor 一處文件錯誤

c3tc3tc3t發表於2017-07-18

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

 

 1 module HairColors
 2   extend ActiveSupport::Concern
 3 
 4 
 5   def self.included(b)
 6     b.class_eval do
 7       mattr_accessor :hair_colors
 8     end
 9     self.class_eval do
10       @@hair_color = nil
11 
12       def self.hair_colors
13         @@hair_color
14       end
15 
16       def self.hair_colors=(obj)
17         @@hair_color = obj
18       end
19     end
20   end
21 end
22 
23 
24 class Person
25   include HairColors
26 end
27 
28 Person.hair_colors = [:brown, :black, :blonde, :red]
29 p Person.hair_colors
30 p Person.new.hair_colors
31 HairColors.hair_colors=23
32 p HairColors.hair_colors
33 
34 p Person.hair_colors
35 p Person.new.hair_colors

 

相關文章