0

I am creating a text-based music player Program.

Inside the read_genre() function which displays the lists of the available genre (which I defined 9 different genres inside instance $genre_names variable array outside of the read_genre() function) and this prompt the user to select a genre for the album, which read in the genre number that user entered. And then the read_genre is called from read_album and print out in print_album function.

The problem is that I am receiving an error 'no implicit conversion of nill into string (TypeError)' line 134 that is puts ' Genre is ' + album.genre from print_album() function.

$genre_names = ['Null','Pop', 'Classic', 'Jazz', 'Rock', 'KPop', 'Metal', 'Punk', 'Romance', 'Latin']

# Display the genre names in a
# numbered list and ask the user to select one
def read_genre()
    length = $genre_names.length
    index = 0
    print $genre_names
  while (index < length)
    select_genre = read_integer_in_range("Select Genre: ", 0,9)
        case select_genre
        when 1
        puts "#{index + 1} " + $genre_names[1]
        break
        when 2
        puts "#{index + 2 } " + $genre_names[2]
        break
        when 3
        puts "#{index + 3} " + $genre_names[3]
        break
        when 4
        puts "#{index + 4} " + $genre_names[4]
        break
        when 5
        puts "#{index + 5} " + $genre_names[5]
        break
        when 6
        puts "#{index + 6} " + $genre_names[6]
        break
        when 7
        puts "#{index + 7} " + $genre_names[7]
        break
        when 8
        puts "#{index + 8} " + $genre_names[8]
        break
        when 9
        puts "#{index + 9} " + $genre_names[9]
        break
         else
          puts 'Please Select again'
          break 
        end
        index
    end

end

def read_album
    # Complete the missing code
  album_title = read_string("Enter Title: ")
  album_artist = read_string("Enter Artist name: ")
  album_genre = read_genre()
  tracks = [tracks]
  album = Album.new(album_title, album_artist, album_genre, tracks)
  album
end


def print_tracks tracks
    # Complete the missing code
    index = 0
    while (index < tracks.length)
     print_track(tracks[index])
     index += 1
    end
end
1
  • This is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? Commented May 19, 2019 at 4:23

1 Answer 1

1

You defined music_file in main method but you're trying to access it inside another method where it's not defined, hence the error.

Let's analyze what your code is doing:

def search_for_track_name(tracks, search_string)
index = 0
count = music_file.gets() 
# Trying to access a variable that is outside of the method's scope
# music_file doesn't exist here.

while (index < count)
    if (tracks[index] == search_string)
        return found_index = -1
        # You are returning -1 if the condition is true, which means
        # returning -1 when the string is found even though you want
        # the exact opposite.
    end
    index += 1
end
 found_index
 # If the pointer gets here, it means that the if statement was never true
 # therefore found_index was never defined

end

Let's address these problems:

def search_for_track_name(tracks, search_string)
index = 0
count = tracks.length
# will loop through the array's items

while (index < count)
    if (tracks[index] == search_string)
        return index
        # will return index if found
    end
    index += 1
end
  -1
  # if the loop finishes and nothing is found, will return -1
end

Or you could just use the index method which returns the index of the first match of an element in an array.

tracks.index(search_string)

https://ruby-doc.org/core-2.6.3/Array.html#method-i-index

Sign up to request clarification or add additional context in comments.

2 Comments

If that's not working you might not be saving your tracks correctly into the array (maybe the strings need to be processed before saving, watch out for line jumps).
I literally just told you. Check what's contained in album.tracks. Are you sure it contains what you say it contains? Maybe you're storing song names as 'song name/song_name.wav' and if that's the case of course it will fail when you ask for 'song name'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.