Avoiding Surprises

Though Ruby is a language that embraces the TIMTOWTDI[6] concept, it is also one that seeks the “Ruby Way” of doing things. In this section are a number of miscellaneous tips to help you move your API in that direction.

Use attr_reader, attr_writer, and attr_accessor

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')
=> #<Message:0x603bf0 @message="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')
=> #<Message:0x5f3c50 @message="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 ...

Get Ruby Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.