Ruby - Как получить доступ к определенным данным в атрибуте класса? - PullRequest
0 голосов
/ 06 ноября 2019

У меня есть программа, которая перечисляет и воспроизводит альбомы. У меня есть:

class Album
    attr_accessor :title, :artist, :genre, :tracks
end
class Track
    attr_accessor :name, :location
end

Мне нужна помощь для выполнения следующей функции:

def play_by_id albums
    puts 'Album ID:'
    user_albumid = gets.to_i
    puts 'Tracks: '
    print_tracks(albums[user_albumid].tracks) # here I want the tracks from the album to be listed. I'm currently using print_tracks which lists both name and location whereas I just want name and maybe a track ID for each track listed eg. 1 - Track 1 Name
    puts 'Track ID:' # after listing the tracks, I want to ask user for the track ID they want to play, then do puts 'Playing track : ' + (track name) + ' by ' + (track artist) 
    # puts 'Playing track ' + 
    # sleep 5
end

Так что, да, просто нужна помощь с кодированием получения названий треков без необходимости получать как названия треков, так и местоположение трековкак у меня с print_tracks:

def print_tracks tracks
    index = 0
    while index < tracks.length
        print_track(tracks[index])
        index += 1
    end
    # print all the tracks use: tracks[x] to access each track.
end
def print_track track
    puts('Track title is: ' + track.name)
    puts('Track file location is: ' + track.location)
end

Это только фрагменты из программы, если вам нужны другие блоки кода, чтобы помочь вам понять, что происходит, просто дайте мне знать, и я опубликую их. :)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...