Моя программа предназначена для получения "Название альбома", "Исполнитель альбома", "Жанр альбома" и "дорожки" из файла .txt. Он помещает эти дорожки в массив и затем печатает этот массив на терминал.
Все работает нормально, за исключением того, что печатаемый массив пуст "[]"
Рубиновый код
module Genre
POP, CLASSIC, JAZZ, ROCK = *1..4
end
$genre_names = ['Null', 'Pop', 'Classic', 'Jazz', 'Rock']
class Album
# NB: you will need to add tracks to the following and the initialize()
attr_accessor :title, :artist, :genre, :tracks
# complete the missing code:
def initialize (title, artist, genre, tracks)
@title = title
@artist = artist
@genre = genre
@tracks = tracks
end
end
class Track
attr_accessor :name, :location
def initialize (name, location)
@name = name
@location = location
end
end
# Returns an array of tracks read from the given file
def read_tracks(music_file)
tracks = Array.new
count = music_file.gets().to_i
index = 0
while index < count
track = read_track(music_file)
tracks << track
index = index + 1
end
tracks
end
# Reads in and returns a single track from the given file
def read_track(music_file)
track_name = music_file.gets
track_location = music_file.gets
track = Track.new(track_name, track_location)
end
# Takes an array of tracks and prints them to the terminal
def print_tracks(tracks)
index = 0
while (index < tracks.length)
puts 'Track Number ' + index.to_s + ' is:'
print_track(tracks[index])
index = index + 1
end
tracks
end
# Reads in and returns a single album from the given file, with all its tracks
def read_album(music_file)
# read in all the Album's fields/attributes including all the tracks
# complete the missing code
album_artist = music_file.gets
album_title = music_file.gets
album_genre = music_file.gets
tracks = read_tracks(music_file)
album = Album.new(album_title, album_artist, album_genre, tracks)
album
end
# Takes a single album and prints it to the terminal along with all its tracks
def print_album(album, tracks)
# print out all the albums fields/attributes
# Complete the missing code.
puts 'Album title is ' + album.title.to_s
puts 'Album artist is ' + album.artist.to_s
puts 'Genre is ' + album.genre.to_s
puts $genre_names[album.genre.to_i]
# print out the tracks
puts 'Tracks are: ' + print_tracks(tracks).to_s
end
# Takes a single track and prints it to the terminal
def print_track(tracks)
tracks.each do |track|
puts('Track title is: ' + track.name.to_s)
puts('Track file location is: ' + track.location.to_s)
end
end
# Reads in an album from a file and then print the album to the terminal
def main
music_file = File.new("album.txt", "r")
if music_file
album = read_album(music_file)
tracks = read_tracks(music_file)
music_file.close
else
puts "unable to read"
end
print_album(album, tracks)
end
main
Это то, что содержит .txt файл
Neil Diamond
Greatest Hits
1
3
Crackling Rose
sounds/01-Cracklin-rose.wav
Soolaimon
sounds/06-Soolaimon.wav
Sweet Caroline
sounds/20-Sweet_Caroline.wav
Я получаю вывод:
Album title is Greatest Hits
Album artist is Neil Diamond
Genre is 1
Pop
Tracks are: []
когда массив дорожек должен распечатывать различные дорожки, их имя и местоположение файла