Ruby Flare

Your awesome Tagline

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

  1. breakthebitorg reblogged this from rubyquicktips and added:
    Nice one, didn’t know this and it’s totally useful.
  2. 26blog reblogged this from rubyquicktips
  3. rubyquicktips reblogged this from rubyflare and added:
    Here’s a method I haven’t seen before: attr_accessor_with_default This ActiveSupport method allows you to set a default...
  4. rubyloveinfo reblogged this from rubyflare
  5. rubyflare posted this