Created
August 3, 2013 19:53
-
-
Save theotherzach/6147757 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
| ## Example 3 ### | |
| class Charlie | |
| def hey(drivel) | |
| answerer(drivel).reply | |
| end | |
| private | |
| def answerer(drivel) | |
| handlers.find {|answer| answer.handles?(drivel)}.new | |
| end | |
| def handlers | |
| [AnswerSilence, AnswerShout, AnswerQuestion, AnswerDefault] | |
| end | |
| end | |
| class AnswerSilence | |
| def self.handles?(input) | |
| input.nil? || input.empty? | |
| end | |
| def reply | |
| 'Fine. Be that way!' | |
| end | |
| end | |
| class AnswerQuestion | |
| def self.handles?(input) | |
| input.end_with?('?') | |
| end | |
| def reply | |
| 'Sure.' | |
| end | |
| end | |
| class AnswerShout | |
| def self.handles?(input) | |
| input == input.upcase | |
| end | |
| def reply | |
| 'Woah, chill out!' | |
| end | |
| end | |
| class AnswerDefault | |
| def self.handles?(input) | |
| true | |
| end | |
| def reply | |
| 'Whatever.' | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment