Skip to content

Instantly share code, notes, and snippets.

@nusco
Created October 4, 2010 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nusco/609946 to your computer and use it in GitHub Desktop.
Save nusco/609946 to your computer and use it in GitHub Desktop.
Post: "The method_missing() Chainsaw"
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
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