module HairColors
mattr_accessor :hair_colors
end
class Person
include HairColors
end
HairColors.hair_colors = [:brown, :black, :blonde, :red]
HairColors.hair_colors # => [:brown, :black, :blonde, :red]
Person.new.hair_colors # => [:brown, :black, :blonde, :red]
Person.hair_colors # undefined method `hair_colors' for Person:Class (NoMethodError)
mattr_accessor 方法可以指定一个默认值,例如:
module Propshaft
mattr_accessor :logger, default: Logger.new(STDOUT)
end