Search for a specific element within an array

I have an array called albums that contains 2 albums. Each album contains an array of tracks and their locations. The arrays are created by reading in the following text file:

2
Greatest Hits
Neil Diamond
1
3
Crackling Rose
sounds/01-Cracklin-rose.wav
Soolaimon
sounds/06-Soolaimon.wav
Sweet Caroline
sounds/20-Sweet_Caroline.wav
Billboard 2019 Rock
Various Artist
4
3
Track 1
sounds/album2/track1.mp3
Track 2
sounds/album2/track2.mp3
Track 3
sounds/album2/track3.mp3

Now, I have written another function, play_selected_track that allows the user to input an album by using the index of the albums array and index of the tracks array within that album. For example, if the user enters 0 when prompted for the album id and then 0 for the track id, it should show that the selected track is Crackling Rose from the album Greatest Hits. However, when I run my code now and enter the same input, I get that the selected track is Greatest Hits from the album Greatest Hits. How can I fix this error?
My enter code:

# TASK 6.3
# Same as Task 6.1, can copy from it here
require './input_functions'

# Genre enumeration
module Genre
    POP, CLASSIC, JAZZ, ROCK = *1..4
  end

  
  
  # Track class
  class Track
      attr_accessor :name, :location
      def initialize (name, location)
          @name = name
          @location = location
      end
  end
  
  
  # Album class
  # Same as Task 6.1, can copy from it here
  class Album
      attr_accessor :title, :artist, :genre, :tracks
      # complete the missing code below:
      def initialize (title, artist, genre, tracks)
        # Same as Task 6.1, can copy from it here
          @title=title
          @artist = artist
          @genre = genre
          @tracks = tracks
      end
  end
  
  
  # Reads in and returns a single track from the given file
  def read_track (music_file)
    # Same as Task 6.1, can copy from it here
     
  track_name=music_file.gets.chomp
  track_location = music_file.gets.chomp
  track = Track.new(track_name, track_location)
  track.name= track_name
  track.location= track_location
    
    
    return track
  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
      # Same as Task 6.1, can copy from it here
      i=0
      while (i<count)
       track = read_track(music_file)
       tracks << track
       i+=1
      end  
      return tracks
  end
  
  
  # Reads in and returns a single album from the given file, with all its tracks
  def read_album(music_file)
      # Same as Task 6.1, can copy from it here
      album_title = music_file.gets
      album_artist = music_file.gets
      album_genre = music_file.gets.chomp.to_i
      
      tracks=read_tracks(music_file)
      album_tracks=tracks


      album = Album.new(album_title, album_artist, album_genre, tracks)
      album.title = album_title
      album.artist = album_artist
      album.genre = album_genre
      album.tracks = album_tracks
      return album
  end
  
  
  # Reads in and returns multiple albums from the given file, with all its tracks
  def read_albums(music_file)
      albums= Array.new() 
      counts = music_file.gets().to_i
      j = 0
      while (j<counts)
        album = read_album(music_file)
        albums<<album
        j+=1
      end
      return albums
    end  
  # Reads in and returns a single album from the given file, with all its tracks
  def read_album(music_file)
    # Same as Task 6.1, can copy from it here
    album_title = music_file.gets
    album_artist = music_file.gets
    album_genre = music_file.gets.to_i
    
    tracks=read_tracks(music_file)
    album_tracks=tracks


    album = Album.new(album_title, album_artist, album_genre, tracks)
    album.title = album_title
    album.artist = album_artist
    album.genre = album_genre
    album.tracks = album_tracks
    return album
end
def print_albums(albums)
index = 0 
while (index<albums.length)

print_album (albums[index])
index +=1

end 
end  
  
  # Takes a single track and prints it to the terminal
  def print_track(track)
      puts(track.name)
      puts(track.location)
  end
  
  # Takes an array of tracks and prints them to the terminal
  def print_tracks(tracks)
    index = 0
    while (index<tracks.length)
      print_track (tracks[index])
      index+=1
     end
  end
  
  
  # Takes a single album and prints it to the terminal along with all its tracks
  def print_album(album)
      # Same as Task 6.1, can copy from it here
      genre_names = ['Null', 'Classic', 'Pop', 'Jazz', 'Rock']
      # print out all the albums fields/attributes
      # Complete the missing code.
		
	    puts album.title
        puts album.artist
        puts('Genre is ' + album.genre.to_s())
        puts(genre_names[album.genre])
        tracks=album.tracks
        print_tracks(tracks)
        # print out the tracks
    
    end
    
  # Takes a file name as string and read the data from it
  def read_albums_file(music_file)
      music_file = File.new("albums.txt", "r")
      albums = read_albums(music_file)
      music_file.close()
      return albums
  end
    def play_selected_track(albums,tracks)
        # ask user to enter ID number of an album in the albums-list
        
        puts "Enter album id:"
        album_id=gets.chomp
        index = 0
		while (index<albums.length)
        if (album_id=="#{index}")
            puts "Please enter track id:"
            track_id=gets.chomp
            j=0
            while (j<tracks.length)
               
                if (track_id=="#{j}")
                    puts "The selected track " + tracks[j].name + "from " + albums[index].title + " is playing"
                end
                j+=1
            end  

		end 
		index+=1
		end 
    end 
  # Reads in an album from a file and then print the album to the terminal
  def main
    
      music_file = File.new("albums.txt", "r")
      albums = read_albums_file(music_file)
      tracks=read_tracks(music_file)
      print_albums(albums)
      music_file.close()
      play_selected_track(albums,tracks)
      
  end
  
  main

There may be other issues, but I don’t understand why you are re-reading the same file as a set of ‘tracks’, and using that in your play method. The tracks seem to be part of each album.

Presumably you want to play track ‘j’ on album ‘index’?

So in your puts statement write albums[index].tracks[j].name and get rid of ‘tracks’ in play_selected_track.

A simpler way to do the work

https://ruby-doc.org/core-2.7.1/Marshal.html

songs = [
[‘Metallica’, ‘Nothing else matters’, 389],
[‘Metallica’, ‘Sad but true’, 325],
[‘Born of Osiris’, ‘The other half of me’, 212]
]

serializing the array

File.open(“songs”, “w”) do |f|

Marshal.dump(songs, f)
end

#retrieving the array:

File.open(“songs”) do |f|
songs = Marshal.load(f)
end