Ruby Flare

Your awesome Tagline

Posts tagged code

19 notes

attr_accessor_with_default

Here’s a method I hadn’t seen before: attr_accessor_with_default

This ActiveSupport method allows you to set a default value for an attribute accessor:

class Person
  attr_accessor_with_default :age, 25
end

some_person.age    # => 25

some_person.age = 26
some_person.age    # => 26

You can even pass in a block. Thanks to @modsognir for finding this one. I wonder why it doesn’t appear in the method list on the Rails API.

(Source: rubydoc.info)

Filed under rails activesupport code