method

thread_mattr_reader

Importance_2
v6.1.7.7 - Show latest stable - 0 notes - Class: Module
thread_mattr_reader(*syms, instance_reader: true, instance_accessor: true, default: nil) public

Defines a per-thread class attribute and creates class and instance reader methods. The underlying per-thread class variable is set to nil, if it is not previously defined.

module Current
  thread_mattr_reader :user
end

Current.user # => nil
Thread.current[:attr_Current_user] = "DHH"
Current.user # => "DHH"

The attribute name must be a valid method name in Ruby.

module Foo
  thread_mattr_reader :"1_Badname"
end
# => NameError: invalid attribute name: 1_Badname

To omit the instance reader method, pass instance_reader: false or instance_accessor: false.

class Current
  thread_mattr_reader :user, instance_reader: false
end

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