sonic-pi

https://github.com/sonic-pi-net/sonic-pi

C++

Code. Music. Live.

Module#mattr_writer

Defines a class attribute and creates a class and instance writer methods to
allow assignment to the attribute.

  module HairColors
    mattr_writer :hair_colors
  end

  class Person
    include HairColors
  end

  HairColors.hair_colors = [:brown, :black]
  Person.class_variable_get("@@hair_colors") # => [:brown, :black]
  Person.new.hair_colors = [:blonde, :red]
  HairColors.class_variable_get("@@hair_colors") # => [:blonde, :red]

If you want to opt out the instance writer method, pass
<tt>instance_writer: false</tt> or <tt>instance_accessor: false</tt>.

  module HairColors
    mattr_writer :hair_colors, instance_writer: false
  end

  class Person
    include HairColors
  end

  Person.new.hair_colors = [:blonde, :red] # => NoMethodError

Also, you can pass a block to set up the attribute with a default value.

  class HairColors
    mattr_writer :hair_colors do
      [:brown, :black, :blonde, :red]
    end
  end

  class Person
    include HairColors
  end

  Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red]

Source | Google | Stack overflow

Edit

git clone [email protected]:sonic-pi-net/sonic-pi.git

cd sonic-pi

open app/server/ruby/vendor/activesupport-7.0.6/lib/active_support/core_ext/module/attribute_accessors.rb

Contribute

# Make a new branch

git checkout -b -your-name--update-docs-Module-mattr_writer-for-pr


# Commit to git

git add app/server/ruby/vendor/activesupport-7.0.6/lib/active_support/core_ext/module/attribute_accessors.rbgit commit -m "better docs for Module#mattr_writer"


# Open pull request

gem install hub # on a mac you can `brew install hub`

hub fork

git push <your name> -your-name--update-docs-Module-mattr_writer-for-pr

hub pull-request


# Celebrate!