Created
February 3, 2011 21:28
-
-
Save carlosbrando/810238 to your computer and use it in GitHub Desktop.
Delegates
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
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