Encapsulation in Ruby

Data Encapsulation in Nature

Data encapsulation restricts access to data or methods from outside the class and keeps the logic internal to the object. For methods, this is achieved in Ruby by setting methods to private. Instance and class variables are encapsulated by default, and cannot be accessed from outside the class. Access can be given by using setter and getter methods. A shortcut in Ruby for such methods are accessors.

The following is an example of a poorly-encapsulated class that uses an attr_accessor:

class BankAccount
attr_accessor :name
  def initialize(name)
    @name = name
  end
end

This is bad practice, because any simple Joe can change the name of my bank account. So how do I change my code, so only authorized people can change the name of my account?

class BankAccount
attr_reader :name

  def initialize(name)
    @name = name
  end

  def change_name(name)
    raise 'no access' unless user.admin?
    @name = name
  end
end

By restricting the renaming of my bank account to admin users, I succesfully encapsulated my data:

  • I only opened it up as far as it is needed for my program.
  • When at a later point I wish to write the name of the account to a database, the user interface remains exactly the same.
Written on October 21, 2015