Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
January 3, 2016 03:09
-
-
Save jpark3000/8400763 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 Vehicle | |
attr_reader :color, :wheels | |
attr_accessor :status | |
def initialize(args = {}) | |
@color = args[:color] | |
@wheels = args[:wheels] | |
@status = status | |
end | |
def drive | |
self.status = :driving | |
end | |
def brake | |
self.status = :stopped | |
end | |
end | |
class Car < Vehicle | |
def initialize(args = {}) | |
super | |
@wheels = 4 | |
end | |
def needs_gas? | |
[true,true,false].sample | |
end | |
end | |
class Bus < Vehicle | |
attr_reader :passengers, :num_seats, :fare | |
def initialize(args) | |
super | |
@num_seats = args[:num_seats] | |
@fare = args[:fare] | |
@passengers=[] | |
end | |
def drive | |
self.brake if stop_requested? | |
super | |
end | |
def admit_passenger(passenger, money) | |
passengers << passenger if money >= fare | |
end | |
def stop_requested? | |
[true,false].sample | |
end | |
def needs_gas? | |
[true,true,true,false].sample | |
end | |
end | |
class Motorbike < Vehicle | |
def initialize(args = {}) | |
super | |
@wheels = 2 | |
end | |
def drive | |
super | |
@speed = :fast | |
end | |
def needs_gas? | |
[true,false,false,false].sample | |
end | |
def weave_through_traffic | |
self.status = :driving_like_a_crazy_person | |
end | |
end |
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 Car | |
@@WHEELS = 4 | |
def initialize(args) | |
@color = args[:color] | |
@wheels = @@WHEELS | |
end | |
def drive | |
@status = :driving | |
end | |
def brake | |
@status = :stopped | |
end | |
def needs_gas? | |
return [true,true,false].sample | |
end | |
end | |
class Bus | |
attr_reader :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 | |
class Motorbike | |
@@WHEELS = 2 | |
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 | |
@status = :driving_like_a_crazy_person | |
end | |
end |
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
require_relative 'inheritance' | |
car = Car.new(color: 'red') | |
bus = Bus.new(color: 'yellow', wheels: 18, num_seats: 32, fare: 2.25) | |
motorbike = Motorbike.new(color: 'black') | |
p car.color == 'red' | |
p car.drive == :driving | |
p car.brake == :stopped | |
x = car.needs_gas? | |
p x == true || x == false | |
p bus.color == 'yellow' | |
p bus.wheels == 18 | |
p bus.num_seats == 32 | |
p bus.fare == 2.25 | |
p bus.admit_passenger("john", 2.50).length == 1 | |
p bus.admit_passenger("bob", 1) == nil | |
p bus.admit_passenger('megan', 2.25).length == 2 | |
p bus.passengers.length == 2 | |
p bus.brake == :stopped | |
y = bus.stop_requested? | |
p !!y == y | |
foo = bus.needs_gas? | |
p !!foo == foo | |
p motorbike.color == 'black' | |
p motorbike.drive == :fast | |
p motorbike.status == :driving | |
p motorbike.brake == :stopped | |
z = motorbike.needs_gas? | |
p !!z == z | |
p motorbike.weave_through_traffic == :driving_like_a_crazy_person | |
# p bus.drive |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment