Caffeine "Sanity" Check!
I should keep my sanity level on check..No excess cup of coffee anymore! Or else..
@cups_of_coffee = 0
def drink_coffee(&block)@cups_of_coffee += 1
yield(@cups_of_coffee) if block_given?
end
case cups_of_coffee
when 0
warn "Low"
when 1
puts "Sane.."
else
raise CaffeineExcessException, "You'll probably go crazy in 1..2..3.."
end
endclass CaffeineExcessException < Exception
def initialize(message)
@message = message
end def to_s
"Fatal: Possible insanity ahead! #@message"
end
end
drink_coffee { |cof| caffeine_sanity_check(cof) } # first call
=> Sane..
drink_coffee { |cof| caffeine_sanity_check(cof) } # second and successive calls
=> CaffeineExcessException: Fatal: Possible insanity ahead! You'll probably go crazy in 1..2..3..
P.S. I'm insane today..
