Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created January 22, 2020 23:19
Show Gist options
  • Select an option

  • Save rodloboz/289b800652df199836f811a8cd656962 to your computer and use it in GitHub Desktop.

Select an option

Save rodloboz/289b800652df199836f811a8cd656962 to your computer and use it in GitHub Desktop.
Advanced OOP Lesson
require 'byebug'
require_relative 'boat'
require_relative 'canoe'
require_relative 'motorboat'
require_relative 'sailboat'
puts "------------ Advanced OOP: Gone Fishin' ------------"
canoe = Canoe.new('Voyager', 16, 3, 1.25)
puts canoe.description
# p canoe.water_volume_displacement # Error private method `water_volume_displacement' called
# you cannot call private methods from outside the class
motorboat = Motorboat.new('Usain Boat', 38, 10, 3.23)
puts motorboat.description
sailboat = Sailboat.new('Black Pearl', 150, 30, 15)
puts sailboat.description
puts "------------"
puts sailboat.skipper? # public interface instance method
sailboat.skipper = "Jack Sparrow" # NoMethodError
puts sailboat.skipper?
puts "The #{sailboat.name}'s skipper is Captain #{sailboat.skipper}"
# puts motorboat.skipper?
# cannot call method defined in inherited sibling classes
puts "------------"
puts "The follow are types of sailboats:"
puts Sailboat.types.join(", ") # class method
# cannot call class method on the instance!!!
p sailboat.types # NotMethodError
class Boat
# Class Constant:
SEAWATER_WEIGHT_PER_CUBIC_FOOT = 64.1
# Getter
attr_reader :name # creates a name public interface
# runs on .new instanciation: Canoe.new
def initialize(name, length, width, depth)
# instance variable with an initial value
@name = name
@length = length
@width = width
@depth = depth
end
# Public Interface (can be called from the outside world):
def displacement
(water_volume_displacement * SEAWATER_WEIGHT_PER_CUBIC_FOOT).round
end
def description
# in instance methods...
# self references/refers to the (specific) instance
"The #{self.name} is a #{self.class.to_s.downcase}. "\
"It has a displacement of #{self.displacement} pounds."
end
# Public Interface ends here
# Private method below:
private
def water_volume_displacement
@length * @width * @depth
end
end
class Butler
# Butler.new(house) => instance of House
def initialize(house)
# in order to create a butler
# we need an instance of House
@house = house # We want @house to store an instance of House
end
def clean_house
# @house is an instance of House
puts "#{@house.name} cleaned!"
end
end
class Canoe < Boat
end
# ThisIsUpperCamelCase # => class / modules names
# this_is_snake_case # => variables, methods and filenames
class House
attr_reader :name, :butler
def initialize(name)
@name = name
# assign an instance of Buttler
# to the house that is being created
@butler = Butler.new(self) # We need to pass the current instance of House
end
end
class Motorboat < Boat
end
class Sailboat < Boat
# attr_writer :skipper
# attr_reader :skipper
attr_accessor :skipper # creates both a reader and a writter
# Defines a Class method
# self here refers to the Class itself
def self.types # Sailboat.types
# self refers to the Sailboat class
%w[Cutter Dinghy Galleon Sloop Schooch]
end
def skipper? # instance method
# self refers to the instance on which
# the method skipper? was called
# @skipper != nil
[email protected]?
end
# Setter
# def skipper=(skipper)
# @skipper = skipper
# end
# Getter
# def skipper
# @skipper
# end
private
def water_volume_displacement
# super - run the parent code (method)
# boat.water_volume_displacement
super + 500
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment