Skip to content

Instantly share code, notes, and snippets.

@carlosbrando
Created February 3, 2011 21:28
Show Gist options
  • Save carlosbrando/810238 to your computer and use it in GitHub Desktop.
Save carlosbrando/810238 to your computer and use it in GitHub Desktop.
Delegates
require "delegate"
class Assistant
def initialize(name)
@name = name
end
def read_email
"(#{@name}) It's mostly spam."
end
def check_schedule
"(#{@name}) You have a meeting today."
end
end
class Manager < DelegateClass(Assistant)
def initialize(assistant)
super(assistant)
end
def attend_meeting
"Please hold my calls."
end
end
frank = Assistant.new("Frank")
anne = Manager.new(frank)
p anne.attend_meeting # => "Please hold my calls."
p anne.read_email # => "(Frank) It's mostly spam."
p anne.check_schedule # => "(Frank) You have a meeting today."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment