DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Use Attr_reader, Attr_writer, And Attr_accessor

02.24.2010
| 17030 views |
  • submit to reddit
        Source: <a href="http://github.com/sandal/rbp-book/raw/gh-pages/pdfs/ch02.pdf">Chapter 2 - Designing Beautiful APIs</a> [github.com] (O’Reilly’s Ruby Best Practices) via <a href="http://www.rubyinside.com/free-chapters-ruby-best-practices-3004.html">RubyInside.com</a>

<snip>
In Ruby, there is no direct external access to the internal state of objects. This means
that it is necessary for you to provide public accessors for your internal objects.
Technically, the following code does that just fine:

class Message
  def initialize(m)
    @message = m
  end
  def get_message
    @message
  end
  def set_message(m)
    @message = m
  end
end

<pre>
>>  m = Message.new('foo')
=>  #<Message:0x603bf0 @message="foo">
>>  m.get_message
=>  "foo"
>>  m.set_message('bar')
=>  "bar"
>>  m.get_message
=>  "bar"
</pre>

However, this approach is almost never seen in code written by practicing Rubyists.
Instead, you’ll see the preceding code example implemented like this:

class Message
  attr_accessor :message
  def initialize(m)
    @message = m
  end
end
<pre>
>>  m = Message.new('foo')
=>  #<Message:0x5f3c50 @message="foo">
>>  m.message
=>  "foo"
>>  m.message = "bar"
=>  "bar"
>>  m.message
=>  "bar"
</pre>

Aside from requiring less typing overall, this code is very clear and expressive, because
it doesn’t include the unnecessary get and set verbs. However, you might wonder how
to do data verification/protection with this approach.
If you need to add some special logic on write, you can still use attr_reader to provide
the reading side of things and then use a custom method to handle the writing:

class Message
  attr_reader :message
  def message=(m)
    @message = m.dup
  end
end

On the other hand, if you need to do some handling on read but can afford to use the
default writer, attr_writer is what you want:
class Message
  attr_writer :message
  def message
    @message.encode!("UTF-8")
  end
end
</snip>