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

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

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