mattr_accessor

Defines both class and instance accessors for class attributes.

If a subclass changes the value then that would also change the value for parent class. Similarly if parent class changes the value then that would change the value of subclasses too.

require 'active_support/all'

module Fruit
  mattr_accessor :price
end

class Apple
  include Fruit
end

apple = Apple.new
apple.price = 100

print Fruit.price
#=> 100

print apple.price
#=> 100

Fruit.price = 200

print Fruit.price
#=> 200

print apple.price
#=> 200

Reference

results matching ""

    No results matching ""