Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Jan 7 ’11

attr_accessor_with_default

Here’s a method I haven’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.

(Source: rubydoc.info)

19 notes 0 comments (via rubyflare)

  1. jonpaullussier reblogged this from bl0ckme
  2. breakbit reblogged this from rubyquicktips and added:
    Nice one, didn’t know this and it’s totally useful. rubyquicktips:
  3. 26blog reblogged this from rubyquicktips
  4. rubyquicktips reblogged this from rubyflare
  5. bl0ckme reblogged this from rubyloveinfo
  6. rubyloveinfo reblogged this from rubyflare
  7. rubyflare posted this

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus