无法识别数组是否包含字符串

问题描述 投票:-1回答:1

我的程序旨在从.txt文件中读取相册信息,一旦用户输入所需的轨道,程序就会检查一个数组以查看它是否包含该轨道。我已成功地将轨道读入数组,但我的程序无法识别字符串是否位于此数组中。

Ruby文件

require './input_functions'
module Genre
  POP, CLASSIC, JAZZ, ROCK = *1..4
end

$genre_names = ['Null', 'Pop', 'Classic', 'Jazz', 'Rock']

class Album
    attr_accessor :title, :artist, :genre, :tracks

    def initialize (title, artist, genre)
        # insert lines here
        @title = title
        @artist = artist
        @genre = genre  
    end
end

class Track
    attr_accessor :name, :location
    def initialize (name, location)
        @name = name
        @location = location
    end
end

def read_track(music_file)
    track_name = music_file.gets
    track_location = music_file.gets
    track = Track.new(track_name, track_location)
end


def read_tracks(music_file)
    tracks = Array.new
    count = music_file.gets.to_i
    index = 0
while index < count
    tracks << read_track(music_file)
    index += 1
end
    tracks
end

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

def read_album music_file
    album_artist = music_file.gets
    album_title = music_file.gets
    album_genre = music_file.gets
    album = Album.new(album_title, album_artist, album_genre)
    album
end


def print_album album

    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(album.tracks).to_s

end


def print_track track
  puts('Track title is: ' + track.name.to_s)
    puts('Track file location is: ' + track.location.to_s)
end

** This is where I am having my problem
# search for track by name.
# Returns the index of the track or -1 if not found
def search_for_track_name(tracks, search_string)
    search_string = gets.chomp
    index = 0
while (index < tracks.length)
    tracks.include?(search_string)
    index = index + 1
end
    if tracks.include?(search_string)
        return index
    else
        index = -1

    end
    return index
end


# Reads in an Album from a file and then prints all the album
# to the terminal

def main

  music_file = File.new("album.txt", "r")
    if music_file
    album = read_album(music_file)
    album.tracks = read_tracks(music_file)
  music_file.close()
    end

  search_string = read_string("Enter the track name you wish to find: ")
  index = search_for_track_name(album.tracks, search_string)
  if index.to_i > -1
    puts "Found " + album.tracks[index].name + " at " + index.to_s
  else
    puts "Entry not Found"
  end
    print_album(album)
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

当用户输入“Sweet Caroline”时,程序应返回已找到该轨道的信息,而不是:

Enter the track name you wish to find:
Sweet Caroline

Entry not Found
Album title is Greatest Hits
Album artist is Neil Diamond
Genre is 1
Pop
Track Number 0 is:
Track title is: Crackling Rose
Track file location is: sounds/01-Cracklin-rose.wav
Track Number 1 is:
Track title is: Soolaimon
Track file location is: sounds/06-Soolaimon.wav
Track Number 2 is:
Track title is: Sweet Caroline
Track file location is: sounds/20-Sweet_Caroline.wav
Tracks are [#<Track:0x000000000331be58 @name="Crackling Rose\n", @location="sounds/01-Cracklin-rose.wav\n">, #<Track:0x000000000331bdb8 @name="Soolaimon\n", @location="sounds/06-Soolaimon.wav\n">, #<Track:0x000000000331bd18 @name="Sweet Caroline\n", @location="sounds/20-Sweet_Caroline.wav\n">]

ruby
1个回答
0
投票

你的问题是album.tracksTrack对象的数组。

而你试图像album.tracks.include?(search_string)一样检查它。当然,不包括在内。

所以你的index = -1"Entry not Found"

您可以使用而不是if tracks.include?(search_string)

if tracks.map(&:name).include?(search_string)

但是你还需要解决另一个问题:你的轨道中的名字现在最终有\n

所以你需要的不是track_name = music_file.gets

track_name = music_file.gets.chomp

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.