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

时间:2019-04-30 12:00:13

标签: ruby

“我的程序”旨在从.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">]

1 个答案:

答案 0 :(得分: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

希望这会有所帮助。