Created
April 7, 2016 16:08
-
-
Save octosteve/67d6a110f87965f019fff9de8dac26f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person | |
attr_reader :name # => nil | |
puts self # => nil | |
def initialize(name) | |
@name = name # => "Bob" | |
end | |
# def self.new | |
# instance = self.allocate | |
# instance.initialize | |
# end | |
def print_self | |
puts self | |
end | |
def inspect | |
self.name # => "Bob" | |
end | |
end | |
bob = Person.new("Bob") # => Bob | |
puts self # => nil | |
# >> Person | |
# >> main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Song | |
attr_reader :title # => nil | |
attr_accessor :artist # => nil | |
def initialize(title) | |
@title = title # => "Work" | |
end | |
def artist=(new_artist) | |
@artist = new_artist # => #<Artist:0x007f93e31d8f70 @name="New Release! Rihanna", @songs=[#<Song:0x007f93e31d8a98 @title="Work", @artist=#<Artist:0x007f93e31d8f70 ...>>]> | |
end | |
end | |
class Artist | |
attr_accessor :name, :songs # => nil | |
def initialize(name) | |
@name = name # => "Rihanna" | |
@songs = [] # => [] | |
end | |
def add_song(song) | |
songs << song # => [#<Song:0x007f93e31d8a98 @title="Work">] | |
update_name # => "New Release! Rihanna" | |
song.artist = self # => #<Artist:0x007f93e31d8f70 @name="New Release! Rihanna", @songs=[#<Song:0x007f93e31d8a98 @title="Work", @artist=#<Artist:0x007f93e31d8f70 ...>>]> | |
# update_artist_for_song(song) # => #<Artist:0x007fab5c045038 @name="New Release! Rihanna", @songs=[#<Song:0x007fab5c044bb0 @title="Work", @artist=#<Artist:0x007fab5c045038 ...>>]> | |
end | |
private # => Artist | |
def update_name | |
self.name = "New Release! #{name}" # => "New Release! Rihanna" | |
end | |
def update_artist_for_song(song) | |
song.artist = self | |
end | |
end | |
rihanna = Artist.new("Rihanna") # => #<Artist:0x007f93e31d8f70 @name="Rihanna", @songs=[]> | |
work = Song.new("Work") # => #<Song:0x007f93e31d8a98 @title="Work"> | |
rihanna.add_song(work) # => #<Artist:0x007f93e31d8f70 @name="New Release! Rihanna", @songs=[#<Song:0x007f93e31d8a98 @title="Work", @artist=#<Artist:0x007f93e31d8f70 ...>>]> | |
rihanna.name # => "New Release! Rihanna" | |
# work.artist.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment