Stuck & Unstuck: Music Library CLI

The Music Library CLI Lab is a lab where one is tasked with creating a music library that is easily accessible to search through, add songs, delete songs, and navigate through the command line interface. This is one of the final projects in the Ruby track for the Flatiron School Community-Based Bootcamp.

Here’s a breakdown of the instructions:

  1. Create an Artist, Song, and Genre class

    • All classes accept a name on initialization
    • All classes have a class variable of an array called all that is accessible via the class method .all
    • All classes have a .destroy_all method that resets the class variable 'all' array to an empty array
    • All classes have a custom constructor of .create
    • All classes have a method #save to save the instance of the class into their class variable 'all' array
  2. Map Relationships Between Classes

    • Song and Artist
      • Songs belong to an artist.
      • Artist has many songs.
      • Adding songs to an artist is done by calling an #add_song method on an instance of the artist class.
      • Songs can be initialized with an optional artist.
    • Song and Genre
    • Artist and Genre
    Now, I was just stuck on the Song#artist= method.

    I just got unstuck by refactoring the Song#artist= method from:
	def artist=(artist)
		#artist.name = @name
		#artist.add_song(@@all)
		self.artist.add_song(self)
	end 

to:

	def artist=(artist)
		#artist.name = @name
		#artist.add_song(@@all)
		if artist == nil
			@artist = artist
		elsif artist != nil
			@artist = artist
			artist.add_song(self)
		end
	end 

Artist#add_song method:
```ruby def add_song(song) @songs.include?(song) ? @songs : @songs << song song.artist == nil ? song.artist = self : @songs end ```

The above edit accounts for what to do if artist is nil, as well as not nil. The problem I was having before is that artist.add_song(self) was not sufficient because I was not defining the artist as equal to an instance variable of @artist first.

Published 24 Aug 2017

founder && full stack && ethereum developer.
Max Goodman on Twitter