首页 > 解决方案 > [] 的未定义方法“名称”:数组 (NoMethodError)

问题描述

我正在创建一个从给定文件中读取曲目和专辑的程序。所以我创建了以下代码。

# 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)

为什么会这样?

标签: ruby

解决方案


上面的五行将一组轨道传递给方法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

旁注:永远不要在方法声明/调用和在​​ 中打开参数列表的括号之间放置空格。它可能会导致无法预料的错误。


推荐阅读