Skip to content

Instantly share code, notes, and snippets.

@fgarcia
Created February 9, 2015 10:02
Show Gist options
  • Save fgarcia/4ce7d571b84d54338e9b to your computer and use it in GitHub Desktop.
Save fgarcia/4ce7d571b84d54338e9b to your computer and use it in GitHub Desktop.
Delegation pitfals vs extend
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