Created
August 4, 2013 02:12
-
-
Save anonymous/6148795 to your computer and use it in GitHub Desktop.
Bob without if's and case
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
class Bob | |
def hey(phrase) | |
think_about(heard(phrase)).respond | |
end | |
def heard(phrase) | |
String(phrase) | |
end | |
def think_about(might_have_heard) | |
THOUGHTS.find(-> { Default }) { |thought| thought.heard?(might_have_heard) } | |
end | |
class << Silent = Object.new | |
def heard?(other) | |
other.empty? | |
end | |
def respond | |
"Fine. Be that way." | |
end | |
end | |
class << Question = Object.new | |
def heard?(other) | |
other.end_with?("?") | |
end | |
def respond | |
"Sure." | |
end | |
end | |
class << Shout = Object.new | |
def heard?(other) | |
other == other.upcase | |
end | |
def respond | |
"Woah, chill out!" | |
end | |
end | |
class << Default = Object.new | |
def respond | |
"Whatever." | |
end | |
end | |
THOUGHTS = [Silent, Question, Shout] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oops, I apparently wasn't logged in when I posted this.