-
-
Save awesome/8696962 to your computer and use it in GitHub Desktop.
This file contains 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 | |
RESPONSES = { | |
:shouting => "Woah, chill out!", | |
:silence => "Fine. Be that way.", | |
:question => "Sure.", | |
:statement => "Whatever.", | |
} | |
def hey(statement) | |
validate_statement(statement) | |
begin | |
RESPONSES[interpret(statement.to_s)] | |
rescue => e | |
raise "ERROR #{e.message}" | |
end | |
end | |
private | |
def validate_statement(statement) | |
raise "ERROR invalid class: statment.class => #{statement.class}; statement must be String" unless statement.is_a? String | |
end | |
def interpret(statement) | |
validate_statement(statement) | |
case | |
when statement.empty? then return :silence | |
when all_uppercase?(statement) then return :shouting | |
when ends_with_question_mark?(statement) then return :question | |
else; return :statement | |
end | |
end | |
def all_uppercase?(statement) | |
validate_statement(statement) | |
statement =~ /^[A-Z]+$/ | |
end | |
def ends_with_question_mark?(statement) | |
validate_statement(statement) | |
statement.end_with?("?") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment