Skip to content

Instantly share code, notes, and snippets.

@rmcsharry
Forked from IanWhitney/final_bob.rb
Last active July 19, 2019 07:56
Show Gist options
  • Save rmcsharry/f8dc8a275c895fca644597cab6a0c2d7 to your computer and use it in GitHub Desktop.
Save rmcsharry/f8dc8a275c895fca644597cab6a0c2d7 to your computer and use it in GitHub Desktop.
class Bob
def reply_to(statement)
public_send("reply_to_#{Statement.build(statement).class}".downcase.to_sym)
rescue NoMethodError
default_reply
end
def reply_to_silence
"Fine. Be that way!"
end
def reply_to_yelling
"Woah, chill out!"
end
def reply_to_question
"Sure."
end
def default_reply
"Whatever."
end
end
class Question < Statement
def self.match?(statement)
statement.end_with?("?")
end
end
class Yelling < Statement
def self.match?(statement)
statement.upcase == statement && statement.downcase != statement
end
end
class Silence < Statement
def self.match?(statement)
statement.strip.empty?
end
end
class NullStatement < Statement
def self.match?(statement)
true
end
end
class Statement
def self.build(statement)
descendants.detect { |klass| klass.match? statement }.new(statement)
end
def self.inherited(klass)
descendants.push klass
end
def self.descendants
@descendants ||= []
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment