Created
July 25, 2016 23:27
-
-
Save FaisalAl-Tameemi/07505c2e8993a50b184679b996bc68b9 to your computer and use it in GitHub Desktop.
OOP Intro
This file contains hidden or 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
require_relative('./vehicle') | |
class Car < Vehicle | |
attr_reader :wheels | |
def initialize(title, capacity, make_year, color, fuel_type, wheels) | |
# remember: calling `super` alone will use the same set of parameters | |
# being passed into the current initialize when calling the parent's initialize | |
super(title, capacity, make_year, color, fuel_type) | |
@wheels = wheels | |
end | |
end |
This file contains hidden or 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 Vehicle | |
attr_reader :capacity, :make_year, :color, :fuel_type | |
attr_accessor :title | |
def initialize(title, capacity, make_year, color, fuel_type) | |
@title = title | |
@capacity = capacity | |
@make_year = make_year | |
@color = color | |
@fuel_type = fuel_type | |
end | |
def info | |
# access title directly from inside the instance | |
puts "This vehicle is called #{@title}" | |
# access `title` through the reader method | |
# puts "This vehicle is called #{self.title}" | |
end | |
# manually create an attribute reader for `make_year` | |
# when using attr_reader, we're creating methods like this | |
# behind the scenes | |
# def make_year | |
# @make_year | |
# end | |
# manually create a setter method for `make_year` | |
# def make_year=(new_value) | |
# @make_year = new_value | |
# end | |
end | |
# to test out how the call works | |
# simply in-class examples | |
# v1 = Vehicle.new("Sam's Jamz", 5, 2005, 'pirate black', 'deisel') | |
# | |
# # .... | |
# | |
# v1.title = "Sam's Boat" | |
# puts v1.title |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment