Dealing with nothing
The null object pattern has gained increased popularity in some parts of the Ruby community in recent years. With the null object pattern, when you deal with another object that may or may not be available, instead of using nil
to represent the case where the other object is not available, you use a separate object that implements the same methods.
As an example of this, let's say you are writing an internal application for a company, and you need to represent employees using an Employee
class. For each employee, you are tracking the name
, position
, phone
, and supervisor
of the employee:
class Employee   attr_reader :name   attr_reader :position   attr_reader :phone   def initialize(name, position, phone, supervisor)     @name = name     @position = position     @phone = phone     @supervisor = supervisor   end
There is...