-
-
Save adkron/6148836 to your computer and use it in GitHub Desktop.
I edited this in the browser so I might not be quite right, but I think I got it.
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 << 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 | |
class Bob | |
def self.add_thought(thought) | |
@thoughts.push thought | |
end | |
@thoughts = [] | |
add_thought Silent | |
add_thought Question | |
add_thought Shout | |
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 | |
def self.thoughts | |
@thoughts | |
end | |
private | |
def thoughts | |
self.class.thoughts | |
end | |
end |
@JEG2 is this new version what you were thinking?
Oh, I read that all wrong. You said to define responses.
respond_with "Fine. Be that way.", when: ->(other) { other.empty? }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@JEG2, thanks. I think that is a good idea. Something like:
I think that could be good. I also like the idea of adding multiple responders. Maybe this would be better to do on an instance?