Last active
September 1, 2015 17:24
-
-
Save just3ws/a39acd9734aed24f6832 to your computer and use it in GitHub Desktop.
FW: FW: FW:
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
require 'forwardable' | |
class Said | |
attr_reader :msg | |
def initialize(msg = nil) | |
@msg = (msg || '').freeze | |
end | |
def loud_enough? | |
(msg =~ /[a-z]/).nil? | |
end | |
def bye? | |
loud_enough? && @msg == 'BYE' | |
end | |
def blank? | |
@msg.strip.empty? | |
end | |
def self.nothing | |
new(nil) | |
end | |
end | |
class Grannie | |
extend Forwardable | |
def initialize(question = Said.nothing) | |
ask question | |
end | |
def ask(question) | |
@question = question | |
end | |
def response | |
return if bye? | |
return 'What was that, honey?' if @question.blank? | |
return "NO, NOT SINCE #{year}!" if @question.loud_enough? | |
'HUH!? SPEAK UP, SONNY!' | |
end | |
def_delegator :@question, :bye? | |
private | |
def year | |
Random.rand(1930..1951) | |
end | |
end | |
grannie = Grannie.new | |
puts grannie.response | |
loop do | |
grannie.ask(Said.new(gets.chomp)) | |
puts grannie.response | |
break if grannie.bye? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment