Created
October 4, 2010 16:04
-
-
Save nusco/609946 to your computer and use it in GitHub Desktop.
Post: "The method_missing() Chainsaw"
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 InformationDesk | |
def emergency | |
# Call emergency... | |
"emergency() called" | |
end | |
def flights | |
# Provide flight information... | |
"flights() called" | |
end | |
def local_transports | |
# Return a metro map... | |
"local_transports() called" | |
end | |
def hotels | |
# Find a suitable hotel... | |
"hotels() called" | |
end | |
# ...even more methods | |
end |
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 DoNotDisturb | |
def initialize | |
@desk = InformationDesk.new | |
end | |
def check_lunch_break | |
hour = Time.now.hour | |
raise "Out for lunch" if hour >= 12 && hour < 14 | |
end | |
def emergency | |
@desk.emergency | |
end | |
def flights | |
check_lunch_break | |
@desk.flights | |
end | |
def local_transports | |
check_lunch_break | |
@desk.local_transports | |
end | |
def hotels | |
check_lunch_break | |
@desk.hotels | |
end | |
# ...and more methods to match the remaining methods in InformationDesk | |
end | |
# At 12:30... | |
DoNotDisturb.new.emergency # => "emergency() called" | |
DoNotDisturb.new.flights # ~> -:32:in `check_lunch_break': Out for lunch (RuntimeError) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment