Skip to content

Instantly share code, notes, and snippets.

@just3ws
Last active September 1, 2015 17:24
Show Gist options
  • Save just3ws/a39acd9734aed24f6832 to your computer and use it in GitHub Desktop.
Save just3ws/a39acd9734aed24f6832 to your computer and use it in GitHub Desktop.
FW: FW: FW:
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