Created
February 9, 2015 10:02
-
-
Save fgarcia/4ce7d571b84d54338e9b to your computer and use it in GitHub Desktop.
Delegation pitfals vs extend
This file contains hidden or 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
require 'delegate' | |
class Person | |
def greeting | |
'hello' | |
end | |
def speak | |
greeting | |
end | |
end | |
class LatinDecorator < SimpleDelegator | |
def greeting | |
"hola" | |
end | |
def listen | |
"OK" | |
end | |
end | |
class GermanDecorator | |
def self.new(object) | |
object.instance_eval do | |
def greeting | |
"Hallo" | |
end | |
end | |
object | |
end | |
end | |
person = Person.new | |
puts LatinDecorator.new(person).speak | |
puts LatinDecorator.new(person).greeting | |
puts GermanDecorator.new(person).speak | |
puts GermanDecorator.new(person).greeting | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment