Lab: Bugs! Download all files, and look at Readme.md for instructions.
def full_library
{
:"U2" => {
:albums => {
:"The Joshua Tree" => {
:songs => ["With or Without You", "Still Haven't Found What I'm Looking For", "Bullet the Blue Sky"]
},
:"Zooropa" =>
:songs => ["Numb"]
}
}
},
:"Talking Heads" => {
:albums => {
:"Fear of Music" => {
:songs => ["Life During Wartime", "Heaven"]
},
:"Speaking in Tongues" => {
:songs => ["This Must Be the Place (Naive Melody)", "Burning Down the House"]
}
}
},
:"Huey Lewis and the News" => {
:albums => {
:"Sports" => {
:songs => ["I Want a New Drug", "If This is It", "Heart of Rock and Roll"]
}
}
}
}
end
require_relative './jukebox.rb'
def run
puts "Welcome to Ruby Console Jukebox!"
while command.downcase(command) != "exit" do
puts "Enter a command to continue. Type 'help' for a list of commands."
command = get_command
run_command(command) unless command.downcase == "exit"
end
end
def get_command
gets.strip
end
def run_command(command)
case command
when "help"
show_help
else
jukebox(command)
end
end
def show_help
help = "Never worked a jukebox, eh? Pretty standard. Available commands are:\n"
help += "'help' - shows this menu\n"
help += "'list' - lists the whole song library\n"
help += "or you can enter an artist's name to show that artist's songs\n"
help = "or you can enter an artist's name to show that artist's songs\n"
puts help
end
run
def jukebox(command)
if command.downcase == "list"
list_library
else
parse_command(command)
end
end
def list_library
lib = full_library
lib.each do |artist, album_hash|
puts list_artist(artist, album_hash)
end
end
def parse_command(command)
parse_artist(command, full_library) || play_song(command, full_library) || not_found(command)
end
def parse_artist(command, lib)
cmd = command.to_sym
parsed = false
if lib.has_key?(cmd)
puts list_artist(command, lib[cmd])
parsed = false
else
lib.each do |artist, hash|
if command.downcase == artist.to_s.gsub("_"," ").downcase
puts list_artist(artist, lib)
parsed = true
break
end
end
end
parsed
end
def play_song(command, lib)
lib.each do |artist, hash|
hash.each do |album_name, albums_hash|
albums_hash.each do |album, songs_hash|
songs_hash.each do |songs|
songs.each do |song|
if song.downcase == command.downcase
puts "Now Playing: #{artist[command].strip}: #{album} - #{song}\n\n"
return true
end
end
end
end
end
false
end
def list_artist(artist, album_hash)
artist_list = "\n---------------\n"
artist_list += "#{artist}:\n"
artist_list += "---------------"
album_hash[:albums].each do |album_name, songs_hash|
artist_list += "\n#{album_name}:\n\t"
artist_list += songs_hash[:songs].join("\n\t")
end
artist_list
end
def not_found(command)
puts "I did not understand '#{command}'!\n\n"
true
end
end
You're new on the job and have been given ownership of the company's jukebox application. Unfortunately, before you can even begin adding cool new features to make it much, much more awesome than it is now, you fire it up and it won't even run! Now you have to go through the code and squash all the bugs (there's a bunch!).
Given the following spec, dive into the code and fix one problem at a time until the program's output matches the spec and there are no errors!
to run the program, type ruby runner.rb
Remember!
When running for the first time, output should be:
Welcome to Ruby Console Jukebox!
Enter a command to continue. Type 'help' for a list of commands.
user enters help
:
Never worked a jukebox, eh? Pretty standard. Available commands are:
'help' - shows this menu
'list' - lists the whole song library
or you can enter an artist's name to show that artist's songs
or you can enter a song title to play that song!
Enter a command to continue. Type 'help' for a list of commands.
user enters list
:
---------------
U2:
---------------
The Joshua Tree:
With or Without You
Still Haven't Found What I'm Looking For
Bullet the Blue Sky
Zooropa:
Numb
---------------
Talking Heads:
---------------
Fear of Music:
Life During Wartime
Heaven
Speaking in Tongues:
This Must Be the Place (Naive Melody)
Burning Down the House
---------------
Huey Lewis and the News:
---------------
Sports:
I Want a New Drug
If This is It
Heart of Rock and Roll
Enter a command to continue. Type 'help' for a list of commands.
user enters U2
OR user enters u2
:
---------------
U2:
---------------
The Joshua Tree:
With or Without You
Still Haven't Found What I'm Looking For
Bullet the Blue Sky
Zooropa:
Numb
Enter a command to continue. Type 'help' for a list of commands.
user enters numb
or Numb
or NUMB
Now Playing: U2: Zooropa - Numb
Enter a command to continue. Type 'help' for a list of commands.
user enters zzyz
:
I did not understand 'zzyz'!
Enter a command to continue. Type 'help' for a list of commands.