Ruby: add an object to the end of an array

I have two objects @tracks (enumerated) and @artist, and I would like to create an enumerated with all the tracks and the artist in them. This means that I can pass them to the method that will be executed (each track and artist has change events):

change_events = object.map(&:change_events).flatten

My idea:

objects = @artist.tracks
objects << @artist

but this gives me this error for the second line (which makes sense, but I don't know how to fix it):

Track(#17816) expected, got Artist(#17572)

Any ideas on how I could do this would be appreciated!

+3
source share
1 answer

This (error) means that it (return value @artist.tracks) is not the array you are dealing with, but with some types of data related to rails. You can try

objects = @artist.tracks.to_a
objects << @artist

, , , .

+5

Source: https://habr.com/ru/post/1788225/


All Articles