ruby - bob (2)

This commit is contained in:
James Walker 2018-10-15 23:12:01 -04:00
parent b430f35082
commit 113fa6679b
Signed by: walkah
GPG Key ID: 3C127179D6086E93

View File

@ -1,18 +1,54 @@
# class Bob
class Bob class Bob
def self.hey(remark) def self.hey(remark)
remark.gsub!(/[[:space:]]+/, '') response = Response.new(remark)
if remark == '' if silence?(remark)
response.silence
elsif shout?(remark)
response.shouted_at
elsif question?(remark)
response.question
else
response.default
end
end
def self.silence?(remark)
remark.strip.empty?
end
def self.shout?(remark)
remark.upcase == remark && remark =~ /[A-Za-z]+/
end
def self.question?(remark)
remark.strip.end_with?('?')
end
end
# Response
class Response
def initialize(remark)
@remark = remark.strip
end
def silence
'Fine. Be that way!' 'Fine. Be that way!'
elsif remark.upcase == remark and remark =~ /[A-Za-z]+/ end
if remark[-1] == '?'
def shouted_at
if @remark.end_with?('?')
"Calm down, I know what I'm doing!" "Calm down, I know what I'm doing!"
else else
'Whoa, chill out!' 'Whoa, chill out!'
end end
elsif remark[-1] == '?' end
def question
'Sure.' 'Sure.'
else end
def default
'Whatever.' 'Whatever.'
end end
end end
end