Decorators allow us to add behavior to objects without affecting other objects of the same class. The decorator pattern is a useful alternative to creating sub-classes.

Read more about Decorator Pattern here.

Read:

You can use SimpleDelegator for building Decorators in Ruby.

NOTE: Please read about the Ruby class SimpleDelegator and its parent class Delegator to understand how it works.

Example:

class User def initialize ( first_name :, last_name :, birthday :) @first_name = first_name @last_name = last_name @birthday = birthday end attr_accessor :first_name , :last_name , :birthday end

class UserDecorator < SimpleDelegator def full_name " #{ first_name } #{ last_name } " end def age # 60 * 60 * 24 * 365.25 is 31557600 # 31557600 is one year in seconds (( Time . now - birthday ) / 31557600 ). floor end end