Learning Class Variables and Methods
By: Johnson Joseph
Tags: Flatiron School, Ruby, Programming, Codingbootcamp, Software Engineering

Song

new

Takes in three arguments: a name, artist, and genre.

The way I was able to complete this first lab was to first create a class called Song.

class Song

end

than define initialize to three arguments — (name, artist, and genre)

I define initialize and set it to three arguments. The reason we set three arguments to initialize is when an instance is created it will instantly provide the name of the song, artist name, and genre the song belongs to.

def initialize(name, artist, genre)

end

2. name

has a name

I created an instant variable @name = name inside the initialize method — Every time an instance Song is created it will instantly provide the name of the song.

def initialize(name, artist, genre)

@name = name

end

3. artist

has an artist

I created an instant variable @artist = artist inside the initialize method — Every time an instance Song is created it will instantly provide the name of the artist.

def initialize(name, artist, genre)

@name = name

@artist = artist

end

4. genre

has a genre

I created an instant variable @genre = genre inside the initialize method. — Every time an instance Song is created it will instantly provide the genre the artist belongs in.

def initialize(name, artist, genre)

@name = name

@artist = artist

@genre = genre

end

5. class variables

has a class variable, @@count

The way to know if a variable is a class variable is based on @@

Class Variable

@@count = 0 # the reason for the zero is because it will be our starting point and currently has no songs.

Class Method

def self.count

@@count

end

6. has a class variable, @@artists, that contains all of the artists of existing songs

I set @@artists to an empty array because no artist has been initialized. Once we start to have songs it will get (<<) pushed into the empty array.

Class Variable

@@artists = [ ]

Class Method

def self.artists

@@artists

end

7. has a class variable, @@genres, that contains all of the genres of existing songs

I set @@genres to an empty array because no genres have been initialized. Once we start to have genres it will get (<<) pushed into the empty array.

Class Variable

@@genres = [ ]

Class Method

def self.genres

@@genres

end

8. is a class method that returns that number of songs created

I created a class variable @@count = 0 this will be our starting point and will take a count of the number of songs it has stored and return the number of songs that was initialized. In order for the class variable to keep count I than had to create a class method .count that returns the entire count of songs.

The way to create a class method is to include self. Self will be referring to the class it’s currently in, inside the method will return @@count which will reference the class variable @@count array and the current songs that are in the array.

def self.count

@@count

end

I then have to create a class variable @@count += 1

The count of songs will increment by 1 as soon as a new song is created, or initialized. We are using the @@count class variable, inside of our #initialize method, which is an instance method. We are saying: when a new song is created, access the @@count class variable and increment its value by 1.

9. is a class method that returns a unique array of artists of existing songs

I had to create a class variable for self.artists, within the self.artists block it will return the entire @@artists array without any duplicates. The reason I was able to return the new @@artists array without duplicates is that of the .uniq (Returns a new array by removing duplicate values in self.)

def self.artists

@@artists.uniq

end

The self. before the method name is a reminder that the method will not be running on a particular artist instance, but will be acting as the factory from which all artists are made: the Song class.

10. .genres

is a class method that returns a unique array of genres of existing songs

.genre_count

I had to create a class variable for genres, within the genres block it will return the entire @@genres array without any duplicates. The reason I was able to return the new @@genres array without duplicates is that of the .uniq

def self.genres

@@genres.uniq

end

11. is a class method that returns a hash of genres and the number of songs that have those genres.

Within the class method of self.genre_count, I created an instance variable genre_count = { } to an empty hash. The empty hash will be the starting point for our hash and will be the key/value of the stored genre and the number of songs. Next @@genres (references the stored genres in the array.) .each will iterate over each genre if a genre is true, it doesn’t match any genre in the hash it moves on to the next line and add 1 to a new key, else if a genre is false it match a genre it will skip the next line and move on to the next line and increment the value by 1 in the matching key. I then return genre_count to get the entire hash key/value.

def self.genre_count

genre_count = {}

@@genres.each do |genre|

if !genre_count[genre]

genre_count[genre] = 1

else

genre_count[genre] += 1

end

end

genre_count

end

12. .artist_count

is a class method that returns a hash of artists and the number of songs that have those artists

Within the class method of self.artist_count, I created an instance variable artist_count ={ } to an empty hash. The empty hash will be the starting point for our hash and will be the key/value of the stored artists and the number of songs. Next @@artists ( references the stored artists in the array.) .each will iterate over each artist if an artist name is true it doesn’t match any name in the hash it will move on to the next line and add 1 to a new key, else if an artist name is false it match a name, it will skip the next line of code and go to the next and increment the value by 1 in the matching key. I then return artist_count to get the entire hash key/value.

def self.artist_count

artist_count = {}

@@artists.each do |artist|

if !artist_count[artist]

artist_count[artist] = 1

else

artist_count[artist] += 1

end

end

artist_count

end