Skip to content

Instantly share code, notes, and snippets.

@joshrowley
Created October 17, 2012 22:05
Show Gist options
  • Save joshrowley/3908612 to your computer and use it in GitHub Desktop.
Save joshrowley/3908612 to your computer and use it in GitHub Desktop.
Object Oriented Jukebox
class Song
attr_accessor :name
@@song_library = []
def add_to_library
@@song_library << self
end
def self.all
@@song_library
end
def initialize(name)
@name = name
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"
]
songs.each do |song|
Song.new(song).add_to_library
end
def help
puts "To see all songs and their corresponding track numbers, use list"
puts "To play a song, use play <track number>"
puts "For example, to play Abducted by The Cults, use play 5"
puts "To exit, use exit"
end
def play(song_selection)
song = Song.all[song_selection]
puts "Playing #{song.name}"
end
def list
Song.all.each_with_index { |song, index|
puts "#{index} => #{song.name}"
}
end
exit = false
puts "Hi, welcome to Jukebox."
while exit == false
print "Enter a command, or type help: "
command = gets.downcase.strip.split
case command.first
when "help"
help
when "play"
song_selection = command[1].to_i
play(song_selection)
when "list"
list
when "exit"
exit = true
else
puts "Sorry, that's not a valid command, type help if you need assistance."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment