Method: Module#mattr_reader
- Defined in:
- lib/core/facets/module/mattr.rb
#mattr_reader(*syms) ⇒ Object
Creates a class-variable attr_reader that can be accessed both on an instance and class level.
c = Class.new do
@@a = 10
mattr_reader :a
end
c.a #=> 10
c.new.a #=> 10
NOTE: This methiod is not a common core extension and is not loaded automatically when using require 'facets'.
CREDIT: David Heinemeier Hansson
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/core/facets/module/mattr.rb', line 192 def mattr_reader( *syms ) syms.flatten.each do |sym| module_eval(<<-EOS, __FILE__, __LINE__) unless defined? @@#{sym} @@#{sym} = nil end def self.#{sym} @@#{sym} end def #{sym} @@#{sym} end EOS end return syms end |