Created
March 5, 2014 21:45
-
-
Save carlossanchezp/9377294 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| METHOD MISSING | |
| class InformationDesk | |
| def emergency | |
| # Call emergency... | |
| "emergency() called" | |
| end | |
| def flights | |
| # Provide flight information... | |
| "flights() called" | |
| end | |
| # ...even more methods | |
| end | |
| Check if a service has been asked during lunch time. | |
| class DoNotDisturb | |
| def initialize | |
| @desk = InformationDesk.new | |
| end | |
| def method_missing(name, *args) | |
| unless name.to_s == "emergency" | |
| hour = Time.now.hour | |
| raise "Out for lunch" if hour >= 12 && hour < 14 | |
| end | |
| @desk.send(name, *args) | |
| end | |
| end | |
| # At 12:30... | |
| DoNotDisturb.new.emergency # => "emergency() called" | |
| DoNotDisturb.new.flights # ~> -:37:in `method_missing': Out for lunch (RuntimeError) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment