Skip to content

Instantly share code, notes, and snippets.

@valachi
Created April 5, 2014 13:12
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 valachi/9991752 to your computer and use it in GitHub Desktop.
Save valachi/9991752 to your computer and use it in GitHub Desktop.
# Bob answers 'Sure.' if you ask him a question.
# He answers 'Woah, chill out!' if you yell at him (ALL CAPS).
# He says 'Fine. Be that way!' if you address him without actually saying anything.
# He answers 'Whatever.' to anything else.
class Bob
def hey(phrase)
responses.fetch(Phrase.new(phrase).type, 'Whatever.')
end
private
def responses
{ shouting: 'Woah, chill out!', question: 'Sure.', silence: 'Fine. Be that way!' }
end
end
class Phrase
attr_reader :phrase
def initialize(string)
@phrase = string
end
def type
silence? || shouting? || question?
end
private
def silence?
phrase.strip.empty? && :silence
end
def shouting?
phrase == phrase.upcase && :shouting
end
def question?
phrase.end_with?('?') && :question
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment