Skip to content

Instantly share code, notes, and snippets.

@DeepNeuralAI
Last active March 1, 2019 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeepNeuralAI/63b51a61574bf0e8ae86a8afa4c8910d to your computer and use it in GitHub Desktop.
Save DeepNeuralAI/63b51a61574bf0e8ae86a8afa4c8910d to your computer and use it in GitHub Desktop.
Class Methods & Inheritance

Class Methods

Quick Recap

Gems:

"can be used to extend or modify functionality in Ruby applications"

Baby


Ruby is an OOP (Almost everything is an object)

Meme

Instance Methods vs Class Methods

Source: Class vs Instance Methods

class SayHello
	# Class Method
  def self.from_the_class
    "Hello, from a class method"
  end
	# Instance Method
  def from_an_instance
    "Hello, from an instance method"
  end
end

Output:

>> SayHello.from_the_class
=> "Hello, from a class method"

>> SayHello.from_an_instance
=> undefined method `from_an_instance' for SayHello:Class


>> hello = SayHello.new
>> hello.from_the_class
=> undefined method `from_the_class' for #<SayHello:0x0000557920dac930>

>> hello.from_an_instance
=> "Hello, from an instance method"

Example

Finish 28_02_19_albums_class_challenge.rb

# Create three classes: Artist, Album and Song
require 'pry'
require 'faker'
## Artist class
class Artist
attr_accessor :albums
attr_reader :num_artists
@@num_artists = 0
def initialize(name)
@name = name
@albums = []
@@num_artists += 1
end
def add_album(album)
@albums << album
end
def album_count
@albums.length
end
def self.count
@@num_artists
end
end
## Album class
class Album
attr_accessor :songs
attr_reader :num_albums
@@num_albums = 0
def initialize(title, release_date)
@title = title
@date = release_date
@songs = []
@@num_albums += 1
end
def add_song(song)
@songs << song
end
def song_count
@songs.length
end
def self.count
@@num_albums
end
end
## Song class
class Song
def initialize(title, duration, genre)
@title = title
@duration = duration
@genre = genre
end
end
## Billboard class
class Billboard
def artist_count
return Artist.count
end
end
## Making 30 random artists
artists = []
albums = []
30.times do
artist = Artist.new(Faker::Music.band)
album = Album.new(Faker::Music.album, (2000..2010).to_a.sample)
5.times { album.add_song(Song.new(Faker::Music::Phish.song, rand(5), Faker::Music.genre))}
artist.add_album(album)
artists << artist
albums << album
end
billboard = Billboard.new
puts "Total number of Artists created: #{billboard.artist_count}"
puts "Total number of Albums created: #{Album.count}"
# Create three classes: Artist, Album and Song
require 'pry'
require 'faker'
## Artist class
class Artist
attr_accessor :albums
attr_reader :num_artists
@@num_artists = 0
def initialize(name)
@name = name
@albums = []
@@num_artists += 1
end
def add_album(album)
@albums << album
end
def album_count
@albums.length
end
def self.count
@@num_artists
end
end
## Album class
class Album
attr_accessor :songs
attr_reader :num_albums
@@num_albums = 0
def initialize(title, release_date)
@title = title
@date = release_date
@songs = []
@@num_albums += 1
end
def add_song(song)
@songs << song
end
def song_count
@songs.length
end
def self.count
@@num_albums
end
end
## Song class
class Song
def initialize(title, duration, genre)
@title = title
@duration = duration
@genre = genre
end
end
## Billboard class
class Billboard
def artist_count
return Artist.count
end
end
## Making 30 random artists
artists = []
albums = []
30.times do
artist = Artist.new(Faker::Music.band)
album = Album.new(Faker::Music.album, (2000..2010).to_a.sample)
5.times { album.add_song(Song.new(Faker::Music::Phish.song, rand(5), Faker::Music.genre))}
artist.add_album(album)
artists << artist
albums << album
end
billboard = Billboard.new
puts "Total number of Artists created: #{billboard.artist_count}"
puts "Total number of Albums created: #{Album.count}"
# binding.pry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment