Skip to content

Instantly share code, notes, and snippets.

@octosteve
Created April 6, 2016 16:21
Show Gist options
  • Save octosteve/568f2aec78161c712480ddabdfa83e5f to your computer and use it in GitHub Desktop.
Save octosteve/568f2aec78161c712480ddabdfa83e5f to your computer and use it in GitHub Desktop.
1.odd? # => true
1 + 1 # => 2
1.+(1) # => 2
1.send(:odd?) # => true
names = ["Steven", "Sophie", "Antoin"] # => ["Steven", "Sophie", "Antoin"]
names[0] # => "Steven"
names.[](0) # => "Steven"
steven = {name: "Steven"} # => {:name=>"Steven"}
steven[:name] # => "Steven"
steven.[](:name) # => "Steven"
:name.zorp! # ~> NoMethodError: undefined method `zorp!' for :name:Symbol
# ~> NoMethodError
# ~> undefined method `zorp!' for :name:Symbol
# ~>
# ~> /Users/StevenNunez/school/web-0416/lectures/03-object-orientation/code/01_everything_is_an_object.rb:11:in `<main>'
work = Object.new # => #<Object:0x007fe91214a0e8>
def work.title
@title # => "Work", "Work"
end
def work.set_title(new_title)
@title = new_title # => "Work"
end
def work.artists
@artists # => ["Rihanna", "Drake"], ["Rihanna", "Drake"]
end
def work.set_artists(new_artists)
@artists = new_artists # => ["Rihanna", "Drake"]
end
def work.describe
"#{self.title} by #{self.artists.join(' and ')}" # => "Work by Rihanna and Drake"
end
def work.duration
@duration # => "3 Minutes"
end
def work.set_duration(new_duration)
@duration = new_duration # => "3 Minutes"
end
ultra_lightbeam = Object.new # => #<Object:0x007fe912149e18>
def ultra_lightbeam.title
@title # => "Ultra Lightbeam", "Ultra Lightbeam"
end
def ultra_lightbeam.set_title(new_title)
@title = new_title # => "Ultra Lightbeam"
end
def ultra_lightbeam.artists
@artists # => ["Kayne West"], ["Kayne West"]
end
def ultra_lightbeam.set_artists(new_artists)
@artists = new_artists # => ["Kayne West"]
end
def ultra_lightbeam.describe
"#{self.title} by #{self.artists.join(' and ')}" # => "Ultra Lightbeam by Kayne West"
end
def ultra_lightbeam.duration
@duration # => "4 Minutes, 1 Second"
end
def ultra_lightbeam.set_duration(new_duration)
@duration = new_duration # => "4 Minutes, 1 Second"
end
work.set_title("Work") # => "Work"
work.title # => "Work"
work.set_artists(["Rihanna", "Drake"]) # => ["Rihanna", "Drake"]
work.artists # => ["Rihanna", "Drake"]
work.describe # => "Work by Rihanna and Drake"
work.set_duration("3 Minutes") # => "3 Minutes"
work.duration # => "3 Minutes"
ultra_lightbeam.set_title("Ultra Lightbeam") # => "Ultra Lightbeam"
ultra_lightbeam.title # => "Ultra Lightbeam"
ultra_lightbeam.set_artists(["Kayne West"]) # => ["Kayne West"]
ultra_lightbeam.artists # => ["Kayne West"]
ultra_lightbeam.describe # => "Ultra Lightbeam by Kayne West"
ultra_lightbeam.set_duration("4 Minutes, 1 Second") # => "4 Minutes, 1 Second"
ultra_lightbeam.duration # => "4 Minutes, 1 Second"
# Song = Class.new do # => Class
class Song
attr_accessor :title, :artists, :duration # => nil
# def title
# @title # => "Work!", "Work!"
# end
#
# def title=(new_title)
# @title = new_title # => "Work!"
# end
#
# def artists
# @artists # => ["Rihanna", "Drake"], ["Rihanna", "Drake"]
# end
#
# def artists=(new_artists)
# @artists = new_artists # => ["Rihanna", "Drake"]
# end
# def duration
# @duration # => "3 Minutes"
# end
#
# def duration=(new_duration)
# @duration = new_duration # => "3 Minutes"
# end
def describe
"#{self.title} by #{self.artists.join(' and ')}" # => "Work! by Rihanna and Drake"
end
end
work = Song.new # => #<Song:0x007f94c20ae688>
work.title = "Work!" # => "Work!"
work.title # => "Work!"
work.artists = ["Rihanna", "Drake"] # => ["Rihanna", "Drake"]
work.artists # => ["Rihanna", "Drake"]
work.describe # => "Work! by Rihanna and Drake"
work.duration = "3 Minutes" # => "3 Minutes"
work.duration # => "3 Minutes"
class Song
PURPOSE = "Use this class to Make songs" # => "Use this class to Make songs"
@@purpose = "The same as above" # => "The same as above"
# @@all = []
attr_accessor :title, :artists, :duration # => nil
def initialize(title: title, artists: artists, duration: duration)
@title = title # => "Work"
@artists = artists # => ["Rihanna", "Drake"]
@duration = duration # => "3 Minutes"
# @@all << self
end
def self.purpose
"Use this class to make Songs"
PURPOSE
@@purpose
end
def describe
"#{self.title} by #{self.artists.join(' and ')}" # => "Work by Rihanna and Drake"
end
end
work = Song.new(title: "Work", artists: ["Rihanna", "Drake"], duration: "3 Minutes") # => #<Song:0x007fa583045fa8 @title="Work", @artists=["Rihanna", "Drake"], @duration="3 Minutes">
Song::PURPOSE # => "Use this class to Make songs"
Song::@@purpose
work.title # => "Work"
work.artists # => ["Rihanna", "Drake"]
work.describe # => "Work by Rihanna and Drake"
work.duration # => "3 Minutes"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment