Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 19, 2015 07:29
-
-
Save auerbachb/5919491 to your computer and use it in GitHub Desktop.
Only an early draft, was reading some related info during most of the 45 minutes that I started this in.
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
#OO_Inheritance.rb | |
# ALL OF MY TESTS ON THE ORIGINAL CODE ARE BELOW, | |
<<-ORIGINAL_CLASSES | |
class Car | |
attr_reader :color, :wheels, :status | |
def initialize(args) | |
@color = args[:color] | |
@wheels = 4 | |
@status = :stopped | |
end | |
def drive | |
@status = :driving | |
end | |
def brake | |
@status = :stopped | |
end | |
def needs_gas? | |
return [true,true,false].sample | |
end | |
end | |
my_car = Car.new(color: 'red') | |
p 'is my car color red like I set it?' | |
p my_car.color == 'red' | |
p 'are my accessors working?' | |
p my_car.wheels == 4 | |
p my_car.status == :stopped | |
p 'does calling drive on my car change it\'s status to driving?' | |
my_car.drive | |
p my_car.status == :driving | |
p 'does calling break on my car change it\'s status to stopped?' | |
my_car.brake | |
p my_car.status == :stopped | |
p 'does calling need gas on my car return a boolean?' | |
gas_status = my_car.needs_gas? | |
p !!gas_status==gas_status | |
class Bus | |
attr_reader :color, :wheels, :num_seats, :fare, :passengers | |
def initialize(args) | |
@color = args[:color] | |
@wheels = args[:wheels] | |
@num_seats = args[:num_seats] | |
@fare = args[:fare] | |
@passengers = [] | |
end | |
def drive | |
return self.brake if stop_requested? | |
@status = :driving | |
end | |
def admit_passenger(passenger, money) | |
@passengers << passenger if money > @fare | |
end | |
def brake | |
@status = :stopped | |
end | |
def stop_requested? | |
return [true,false].sample | |
end | |
def needs_gas? | |
return [true,true,true,false].sample | |
end | |
end | |
p 'does my initialization and my getter work?' | |
my_bus = Bus.new(color: 'yellow', wheels: 6, num_seats: 30, fare: 2) | |
p my_bus.color == 'yellow' | |
p my_bus.wheels == 6 | |
p my_bus.num_seats == 30 | |
p my_bus.fare == 2 | |
p 'can a passenger get on if money > fare?' | |
my_bus.admit_passenger("Brett",10) | |
p my_bus.passengers.include?("Brett") | |
p 'does a passenger get rejected if $<fare?' | |
my_bus.admit_passenger("brokeguy", 1) | |
p !(my_bus.passengers.include?("brokeguy")) | |
p 'do the breaks work?' | |
p my_bus.brake == :stopped | |
p 'does calling need gas on my bus return a boolean?' | |
gas_status = my_bus.needs_gas? | |
p !!gas_status==gas_status | |
class Motorbike | |
attr_reader :wheels, :speed, :driving | |
@@WHEELS = 2 #this will not work with inheritance, so we'll have to kill it | |
def initialize(args) | |
@color = args[:color] | |
@wheels = @@WHEELS | |
end | |
def drive | |
@status = :driving | |
@speed = :fast | |
end | |
def brake | |
@status = :stopped | |
end | |
def needs_gas? | |
return [true,false,false,false].sample | |
end | |
def weave_through_traffic | |
@driving = :status_like_a_crazy_person | |
end | |
end | |
p 'does the bike always have 2 wheels' | |
p my_bike = Motorbike.new(color: 'red') | |
p your_bike = Motorbike.new(color: 'black') | |
p my_bike.wheels == your_bike.wheels | |
p 'does the bike go fast?' | |
my_bike.drive | |
p my_bike.speed == :fast | |
p ' does the bike go crazy?' | |
my_bike.weave_through_traffic | |
p my_bike.driving == :status_like_a_crazy_person | |
ORIGINAL_CLASSES | |
#REFACTORED TO USE INHERITANCE | |
class Vehicle | |
attr_reader :status | |
def initialize(args) | |
@color = args.fetch(:color, 'red') | |
@wheels = args.fetch(:wheels, 4) | |
@refuel_frequency = [true,false] | |
end | |
def drive | |
@status = :driving | |
end | |
def brake | |
@status = :stopped | |
end | |
def needs_gas? | |
return @refuel_frequency.sample | |
end | |
end | |
class Car < Vehicle | |
def initialize(args) | |
super(args) #POOTR suggests not using super, see book for using hooks | |
@refuel_frequency = [true,true,false] | |
end | |
end | |
p my_car = Car.new(color: "blue") | |
p my_car.drive == :driving | |
p my_car.brake == :stopped | |
p my_car.needs_gas? | |
class Bus < Vehicle | |
attr_reader :passengers | |
def initialize(args) | |
super(args) | |
@num_seats = args[:num_seats] | |
@fare = args[:fare] | |
@passengers=[] | |
@refuel_frequency = [true,true,true,false] | |
end | |
def drive | |
return self.brake if stop_requested? | |
super | |
end | |
def admit_passenger(passenger,money) | |
@passengers << passenger if money > @fare | |
end | |
end | |
p my_bus = Bus.new({color: "blue", wheels: 4, num_seats: 12, fare: 2.5}) | |
p my_bus.admit_passenger("a_person",10) | |
p my_bus.passengers[0] == "a_person" | |
p my_bus.brake == :stopped | |
p my_bus.needs_gas? | |
class Motorbike < Vehicle | |
def initialize(args) | |
super(args) #POOTR suggests not using super, see book for using hooks | |
@refuel_frequency = [true,false,false,false] | |
end | |
def drive | |
super | |
@speed = :fast | |
end | |
def weave_through_traffic | |
@status = :driving_like_a_crazy_person | |
end | |
end | |
p my_bike = Motorbike.new(wheels: 2, color: "blue") | |
p my_bike.drive == :fast | |
p my_bike.brake == :stopped | |
p my_bike.weave_through_traffic == :driving_like_a_crazy_person | |
p my_bike.needs_gas? | |
#Vehicle is the superclass. Cars, busses and motorbikes are all vehicles. | |
#This can be seen in the shared attributes (color, wheels), and methods | |
#(drive, brake, need_gas?). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment