method

thread_mattr_accessor

Importance_2
v5.2.3 - Show latest stable - 0 notes - Class: Module
thread_mattr_accessor(*syms) public

Defines both class and instance accessors for class attributes.

class Account
  thread_mattr_accessor :user
end

Account.user = "DHH"
Account.user     # => "DHH"
Account.new.user # => "DHH"

If a subclass changes the value, the parent class’ value is not changed. Similarly, if the parent class changes the value, the value of subclasses is not changed.

class Customer < Account
end

Customer.user = "Rafael"
Customer.user # => "Rafael"
Account.user  # => "DHH"

To opt out of the instance writer method, pass instance_writer: false. To opt out of the instance reader method, pass instance_reader: false.

class Current
  thread_mattr_accessor :user, instance_writer: false, instance_reader: false
end

Current.new.user = "DHH"  # => NoMethodError
Current.new.user          # => NoMethodError

Or pass instance_accessor: false, to opt out both instance methods.

class Current
  mattr_accessor :user, instance_accessor: false
end

Current.new.user = "DHH"  # => NoMethodError
Current.new.user          # => NoMethodError
Show source
Register or log in to add new notes.