Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 19, 2015 07:28
-
-
Save bhancock96/5918558 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 | |
def initialize(args) | |
@color = args[:color] | |
@wheels = 4 | |
end | |
end | |
class Car < Vehicle | |
attr_reader | |
def initialize(args) | |
super(args) | |
end | |
def drive | |
@status = :driving | |
end | |
def brake | |
@status = :stopped | |
end | |
def needs_gas? | |
return [true,true,false].sample | |
end | |
end | |
class Bus < Vehicle | |
attr_reader :passengers | |
def initialize(args) | |
super(args) | |
@wheels = 8 | |
@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 < Vehicle | |
def initialize(args) | |
super(args) | |
@wheels = 2 | |
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 | |
car = Car.new(color:"white") | |
bus = Bus.new(color:"yellow", wheels:8, num_seats:30, fare:1) | |
motorbike = Motorbike.new(color:'blue') | |
# Car Tests | |
p car.instance_eval{@color} == "white" | |
p car.drive == :driving | |
p car.brake == :stopped | |
p car.instance_eval{@wheels} == 4 | |
# Bus Tests | |
p bus.instance_eval{@color} == "yellow" | |
p bus.instance_eval{@wheels} == 8 | |
p bus.instance_eval{@num_seats} == 30 | |
p bus.brake == :stopped | |
# Motorbike Tests | |
p motorbike.instance_eval{@wheels} == 2 | |
p motorbike.brake == :stopped | |
p motorbike.instance_eval{@color} == "blue" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment