Skip to content

Instantly share code, notes, and snippets.

@aviflombaum
Forked from joshrowley/oo_jukebox.rb
Created October 18, 2012 00:53
Show Gist options
  • Save aviflombaum/3909258 to your computer and use it in GitHub Desktop.
Save aviflombaum/3909258 to your computer and use it in GitHub Desktop.
Object Oriented Jukebox (with Session class)
class Song
attr_accessor :name
@@song_library = []
def self.all
@@song_library
end
def initialize(name)
@name = name
@@song_library << self
end
end
class Session
attr_accessor :status, :user_input
def initialize
@status = true
puts "Hi, welcome to Jukebox."
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 = self.user_input[1].to_i
song = Song.all[song_selection]
puts "Playing #{song.name}"
end
def prompt
print "Enter a command, or type help: "
self.user_input = gets.downcase.strip.split
command = self.user_input.first
if self.respond_to?(command.to_sym)
self.send(command.to_sym)
else
puts "Sorry, that's not a valid command, type help if you need assistance."
end
end
def list
Song.all.each_with_index { |song, index|
puts "#{index} => #{song.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 { |song| Song.new(song) }
session = Session.new
while session.status
session.prompt
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment