我的应用程序需要从文件中读取数据,然后用户可以搜索该数据以查看其是否存在。该文件读取相册的ID,然后用户必须输入该ID才能访问它
文本文件
Marcus Hospin
Raw
3 - Genre Value
1 - ID
Sag My Pants
sounds/01-Sag-My-Pants.wav
The Purge
sounds/06-The-Purge.wav
Ill Mind 8
sounds/20-Ill-Mind-8.wav
红宝石代码
class Album
attr_accessor :title, :artist, :genre, :id, :tracks
def initialize (title, artist, genre, id)
@title = title
@artist = artist
@genre = genre
@id = id
end
end
def read_album(music_file)
album_artist = music_file.gets
album_title = music_file.gets
album_genre = music_file.gets
album_id = music_file.gets
album = Album.new(album_title, album_artist, album_genre, album_id)
album
end
def main
album = read_album(music_file)
puts 'Enter the ID of the album you wish to play'
search_string = gets.to_s
album.map(&:id).include?(search_string) || -1
if index != -1
puts "Found, playing album " + album[index].id + " at " + index.to_s
else
puts "Entry not Found"
end
end
main
程序的预期结果是返回“找到,正在1(索引)播放专辑1(ID)。但是实际输出是undefined method `map' for #<Album:0x0000000003357ca0> (NoMethodError)
答案 0 :(得分:0)
map
是未定义的,因为您在单个对象而不是对象集合上调用它。您需要先用Album
个对象填充数组,然后才能map
对其进行覆盖。