我正在创建一个程序,可以从给定文件中读取曲目和专辑。因此,我创建了以下代码。
# Task 6.1 T - use the code from last week's tasks to complete this:
# eg: 5.1T, 5.2T
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
tracks
end
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 = music_file.gets
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_track(tracks).to_s
end
# Takes a single track and prints it to the terminal
def print_track (track)
# This is the line where the error is directing me
puts('Track title is: ' + track.name.to_s)
puts('Track file location is: ' + track.location.to_s)
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")
album = read_album(music_file)
tracks = read_tracks(music_file)
read_tracks(music_file)
print_album(album, tracks)
print_tracks(tracks)
end
main
该程序应该从给定文件中读取曲目,但出现错误:
C:/Users/Harry/Desktop/6.1T/album_file_handling.rb:104:in `print_track': undefined method `name' for []:Array (NoMethodError)
为什么会这样?
答案 0 :(得分:1)
上方的五行代码将轨道数组传递给方法print_track
:
puts 'Tracks are ' + print_track(tracks).to_s
在方法本身内部,您希望在那里有单个轨道。您需要遍历轨道并打印每个轨道。这样的事情会做:
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
边注:永远不要在方法声明/调用和括号之间插入空格,以打开ruby中的参数列表。可能会导致意外错误。
答案 1 :(得分:1)
问题是您要将数组(轨道)传递给print_album def中的print_track。
def print_album (album, tracks)
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]
tracks.each do |track|
puts 'Tracks are '
print_track(track).to_s
end
end