Make Your First Text Adventure in Ruby

Designing Trustworthy Objects

Objects in ruby are much like people. They have aspects which they present to the outside world, and some aspects which they keep to themselves. In ruby, the publicly accessible aspects of an object are known as its interface. Regardless of how many methods or attributes an object really has, its interface describes the ones that other objects are allowed to know about.

The access level of your object's methods are controlled by two key words: private and protected.

class Wanderer
  # This method is part of the Wanderer's public interface.
  def greet; end

  # This method is protected. It is only accessible from
  # within instances of the Wanderer class or any
  # subclasses of Wanderer.
  protected
  def tell_story; end

  # This method is private. It is only accessible from within instances
  # of the Wanderer class.
  private
  def tell_secret; end
end

To create a robust and maintainable system, it is best to ensure that the public interfaces of your objects are carefully curated. A good rule of thumb is that your public interface should be stable, but the private interface on which it depends can be more dynamic. If your public interface changes as infrequently as possible, and you only ever depend on the public interface, change is unlikely to cause widespread breakage.

The road to maintenance nirvana is paved with classes that depend on things that change less often than they do.

~ Sandi Metz, Practical Object-Oriented Design In Ruby