Created
January 15, 2015 05:03
-
-
Save sbrady/9b02c9cfc6609476ccd3 to your computer and use it in GitHub Desktop.
Example decorator pattern in Ruby using simple delegate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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