Method: Module#cattr_writer
- Defined in:
- lib/core/facets/module/mattr.rb
#cattr_writer(*syms) ⇒ Object
Creates a class-variable attr_writer that can be accessed both on an instance and class level.
class CAWExample
cattr_writer :a
def self.a
@@a
end
end
CAWExample.a = 10
CAWExample.a #=> 10
CAWExample.new.a = 29
CAWExample.a #=> 29
NOTE: This method is not a common core extension and is not loaded automatically when using require 'facets'.
CREDIT: David Heinemeier Hansson
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/core/facets/module/mattr.rb', line 94 def cattr_writer(*syms) syms.flatten.each do |sym| module_eval(<<-EOS, __FILE__, __LINE__) unless defined? @@#{sym} @@#{sym} = nil end def self.#{sym}=(obj) @@#{sym} = obj end def #{sym}=(obj) @@#{sym}=(obj) end EOS end return syms end |