Platinum Partner

Use Attr_reader, Attr_writer, And Attr_accessor

Source: Chapter 2 - Designing Beautiful APIs [github.com] (O’Reilly’s Ruby Best Practices) via RubyInside.com


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


>>  m = Message.new('foo')
=>  #
>>  m.get_message
=>  "foo"
>>  m.set_message('bar')
=>  "bar"
>>  m.get_message
=>  "bar"
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
>>  m = Message.new('foo')
=>  #
>>  m.message
=>  "foo"
>>  m.message = "bar"
=>  "bar"
>>  m.message
=>  "bar"
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
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}