class Song
attr_reader :name
def initialize(name)
@name = name
end
end
imagine = Song.new('Imagine')
hey_jude = Song.new('Hey Jude')
baby = Song.new('Baby')
class Playlist
attr_reader :songs
def initialize
@songs = songs
end
def add_song(song)
songs << song
end
end
class Playlist
attr_reader :songs
def initialize
@songs = songs
end
def add_song(song)
songs << song
end
end
favourties = Playlist.new
favourites.add_song(imagine)
favourites.add_song(hey_jude)
favourites.add_song(baby)
favourites.songs.size => 3
class Playlist
attr_reader :index, :songs
def initialize
@songs = songs
@index = 0
end
def next
# Start again if this is the last song
last_song? ? index = 0 : index += 1
end
def previous
# Go to the end if this is the first song
first_song? ? index = songs.size : index -= 1
end
def current_song
songs[index]
end
def add_song(song)
songs << song
end
private
def last_song?
index < songs.length
end
def first_song?
index == 0
end
end