Created
March 10, 2016 12:21
-
-
Save ntijoh-daniel-berg/855519d732db9de8aff6 to your computer and use it in GitHub Desktop.
This file contains 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
class CountdownTooShortError < Exception ; end | |
# TOMDOC | |
# Public: Creates a list of numbers from 'from' to zero | |
# | |
# from - The Fixnum to count down from | |
# | |
# raises TypeError if from is not a Fixnum | |
# raises ArgumentError if... | |
# Examples | |
# countdown(from: 3) #=> [3,2,1,0] | |
# | |
# Returns the list of numbers | |
# YARDDOC | |
# Creates a list of numbers from 'from' to zero | |
# @param from [Fixnum] the number to count down from | |
# @raise [TypeError] if from is not a fixnum | |
# @raise [ArgumentError] if... | |
# @return A list of numbers from 'from' to zero | |
# @example countdown(from: 3) #=> [3,2,1,0] | |
def countdown(from:) | |
raise TypeError, 'from must be a Fixnum' unless from.is_a? Fixnum | |
raise ArgumentError, 'from must not be negative' if from < 0 | |
raise CountdownTooShortError, 'can not count down from zero' if from == 0 | |
current = from | |
numbers = [] | |
while current >= 0 | |
numbers << current | |
current -= 1 | |
end | |
return numbers | |
end | |
def audio_countdown | |
begin | |
countdown(from: 0).each do |number| | |
system("say #{number}") | |
sleep 1 | |
end | |
rescue ArgumentError => e | |
system("say Can not initiate countdown") | |
rescue CountdownTooShortError => e | |
system("say well, shiiiit!") | |
system("say boom!") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment