Skip to content

Instantly share code, notes, and snippets.

@ntijoh-daniel-berg
Last active March 10, 2016 11:54
Show Gist options
  • Save ntijoh-daniel-berg/df35f634b967f2cc6ee6 to your computer and use it in GitHub Desktop.
Save ntijoh-daniel-berg/df35f634b967f2cc6ee6 to your computer and use it in GitHub Desktop.
def list_files_in_dir
files = Dir.glob('*.*')
files.each_with_index do |file, i|
puts "#{i + 1}: #{file}"
end
end
def ask_for_filename
print 'Enter name of file to open '
filename = gets.chomp
return filename
end
def display_file(filename:)
if File.exists?(filename)
File.readlines(filename).each_with_index do |line, i|
puts "#{i + 1}: #{line}"
end
else
puts "Cannot find file #{filename}"
puts "Please try again"
puts "These files are available:"
list_files_in_dir
filename = ask_for_filename
display_file(filename: filename)
end
# Exceptions should be EXCEPTIONAL
# begin
# File.readlines(filename).each_with_index do |line, i|
# puts "#{i + 1}: #{line}"
# end
# rescue Errno::ENOENT => error
# puts "Cannot find file #{filename}"
# puts "Please try again"
# puts "These files are available:"
# list_files_in_dir
# filename = ask_for_filename
# display_file(filename: filename)
# end
end
list_files_in_dir
filename = ask_for_filename
display_file(filename: filename)
class CountdownTooShortError < Exception ; end
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
audio_countdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment