Skip to content

Instantly share code, notes, and snippets.

@sbrady
Created January 15, 2015 05:03
Example decorator pattern in Ruby using simple delegate
class Person
attr_accessor :first_name, :last_name
def initialize(first_name, last_name)
self.first_name = first_name
self.last_name = last_name
end
end
require 'delegate'
class PersonDecorator < SimpleDelegator
def full_name
"#{first_name} #{last_name}"
end
end
person = Person.new('Sean', 'Brady')
PersonDecorator.new(person).full_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment