This provides
an easy shortcut to test for missing values??”for example, a string can be tested with s.blank?
rather than (s.nil? || s.empty?).
Class Attribute Accessors core_ext/class/attribute_accessors.rb
ActiveSupport extends the Class class to provide friendly attribute accessors specific to a
Class object. These attribute accessors (cattr_reader, cattr_writer, and cattr_accessor)
mirror the attr_reader, attr_writer, and attr_accessor methods on Module. They define
accessors at both the class and instance level.
class C
cattr_accessor :log
self.log = ""
def initialize
log << "#{self.inspect} created\n"
end
end
3.times {C.new}
puts C.log
# >> #
created
# >> # created
# >> # created
These methods use class variables instead of instance variables:
C.log == (class C; @@log; end) # => true
There are also module-level attribute accessors: mattr_reader, mattr_writer, and mattr_
accessor.
Class Inheritable Attributes core_ext/class/inheritable_attributes.rb
Inheritable attributes are attributes defined on a class object (as with class attribute accessors).
However, the attributes and their values are cloned to children when the class is
subclassed. There are three flavors of inheritable attributes: normal, array, and hash. As
with class attribute accessors, accessor methods are defined at both the class and instance
level, so they can be accessed from instances of the class.
Pages:
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105