method
attr_accessor_with_default
v3.0.9 -
Show latest stable
- Class:
Module
attr_accessor_with_default(sym, default = nil, &block)public
Declare an attribute accessor with an initial default return value.
To give attribute :age the initial value 25:
class Person attr_accessor_with_default :age, 25 end some_person.age => 25 some_person.age = 26 some_person.age => 26
To give attribute :element_name a dynamic default value, evaluated in scope of self:
attr_accessor_with_default(:element_name) { name.underscore }
1Note
Some unexpected behaviour
When using Array as default value, it behaves more like cattr_accessor...
class A
attr_accessor_with_default :b, [] end
x = A.new
x.b << 1
#puts x.b.inspect => [1]
y = A.new
y.b << 2
#puts y.b.inspect => [1, 2]