Skip to content

Instantly share code, notes, and snippets.

@chhhris
Created June 10, 2013 18:29
Show Gist options
  • Save chhhris/5751067 to your computer and use it in GitHub Desktop.
Save chhhris/5751067 to your computer and use it in GitHub Desktop.
# jukebox.rb
class Jukebox
attr_accessor :songs
def initialize(songs)
@songs = songs
end
def list
index = 1
@songs.each do |song|
puts "Song #{index}: #{song}"
index += 1
end
puts "\n>> Would you like to play a song in this list? if so, type 'play'"
end
def play
#call list
puts "\n>> Enter song number."
# puts "\n>> Type 'Help' for more info. If all these songs suck, type 'Exit' to leave the program."
song_number = gets.chomp.downcase.strip
if song_number == 'help'
help
elsif song_number == 'exit'
exit
elsif song_number == 'list'
list
else
until song_number.to_i >= 1 && song_number.to_i <= 7
puts ">> Error: please enter an integer from 1 - #{@songs.length}."
song_number = gets.chomp.downcase.strip.to_i
end
song = @songs[song_number.to_i - 1]
puts "\nNow playing: #{song}."
puts "\n>> Type 'List' to pick a new song. Type 'Exit' to take a break."
end
end
def help
puts "\n>> HELP SCREEN"
puts ">> Type 'List' to display the songs. Type 'Play' to play a song."
puts ">> Type 'Exit' to leave the program."
end
def exit
exit
end
end
songs = [
"The Phoenix - 1901",
"Tokyo Police Club - Wait Up",
"Sufjan Stevens - Too Much",
"The Naked and the Famous - Young Blood",
"(Far From) Home - Tiga",
"The Cults - Abducted",
"The Phoenix - Consolation Prizes"
]
jukebox1 = Jukebox.new(songs) #capital J is the class, jukebox1 is an instance of that class
# list, play, help, exit
# First thing - ask user what they would like to do
puts "Welcome to Team Rogue's Jukebox."
puts ">> Type 'List' to display the songs."
puts ">> Type 'Help' for more info. Type 'Exit' to leave the program."
input = gets.chomp.downcase.strip
until input == "exit"
#do stuff
case input
when "list"
jukebox1.list
when "play"
jukebox1.play
when "help"
jukebox1.help
else
puts ">> Command not recognized. Available commands are 'List', 'Play', 'Help', and 'Exit'"
end
input = gets.chomp.downcase.strip
end
puts "\nThanks for playing our Jukebox! Have a great day."
exit
puts "This should never show"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment