Skip to content

Instantly share code, notes, and snippets.

@thenickcox
Created August 12, 2013 16:57
Show Gist options
  • Save thenickcox/6212813 to your computer and use it in GitHub Desktop.
Save thenickcox/6212813 to your computer and use it in GitHub Desktop.
exercism.io solution to first problem
class Bob
def hey(str)
@string = StringInterpreter.new(str)
handle_responses
end
private
def handle_responses
%w(blank exclamation question).each do |type|
return responder.send("#{type}_response") if @string.send("#{type}?")
end
responder.default_response
end
def responder
Responder.new
end
end
class StringInterpreter
attr_reader :string
def initialize(string)
@string = string
end
def blank?
string.nil? || string.empty?
end
def exclamation?
string.upcase == string
end
def question?
string =~ /\?$/
end
end
class Responder
def blank_response
'Fine. Be that way.'
end
def exclamation_response
'Woah, chill out!'
end
def question_response
'Sure.'
end
def default_response
'Whatever.'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment