自己在本地实现 rails api 中的实例,但是报错,api 中的实例如下所示:
module HairColors
mattr_accessor :hair_colors
end
class Person
include HairColors
end
Person.hair_colors = [:brown, :black, :blonde, :red]
Person.hair_colors # => [:brown, :black, :blonde, :red]
Person.new.hair_colors # => [:brown, :black, :blonde, :red]
自己本地进行实现:
require 'active_support'
module HairColors
mattr_accessor :hair_colors
end
class Person
include HairColors
end
Person.hair_colors = [:brown, :black, :blonde, :red] #报错 undefined method `hair_colors=' for Person:Class (NoMethodError)
Person.hair_colors # => [:brown, :black, :blonde, :red]
Person.new.hair_colors # => [:brown, :black, :blonde, :red]
自己的实现与 api 中的实现结果不符,为何?