-
-
Save tambling/5760659 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, :status | |
def initialize(args) | |
@wheels=4 | |
@fuel_economy=[true, false] | |
@color = args[:color] | |
end | |
def drive | |
@status=:driving | |
end | |
def brake | |
@status=:stopped | |
end | |
def needs_gas? | |
return @fuel_economy.sample | |
end | |
end | |
class Car<Vehicle | |
def initialize(args) | |
super | |
@fuel_economy=[true, false, false] | |
end | |
end | |
class Bus<Vehicle | |
attr_reader :passengers | |
def initialize(args) | |
super | |
@fuel_economy=[true, true, true, false] | |
@wheels=args[:wheels] | |
@num_seats=args[:num_seats] | |
@fare=args[:fare] | |
@passengers=[] | |
end | |
def drive | |
return self.brake if stop_requested? | |
super | |
end | |
def admit_passenger(passenger,money) | |
@passengers << passenger if money > @fare | |
end | |
def stop_requested? | |
return [true,false].sample | |
end | |
end | |
class Motorbike<Vehicle | |
attr_reader :speed | |
def initialize(args) | |
super | |
@wheels=2 | |
@fuel_economy=[true,false,false,false] | |
end | |
def drive | |
super | |
@speed=:fast | |
end | |
def weave_through_traffic | |
@status= :driving_like_a_crazy_person | |
end | |
end | |
car=Car.new( color: "blue") | |
puts car.color=="blue" | |
puts car.wheels==4 | |
car.drive | |
puts car.status==:driving | |
car.brake | |
puts car.status==:stopped | |
bus=Bus.new(color: "red", wheels: 6, num_seats: 30, fare: 2) | |
puts bus.color=="red" | |
bus.admit_passenger("Jim Bob", 3) | |
puts bus.passengers==["Jim Bob"] | |
bus.drive | |
puts bus.status==:driving || bus.status==:stopped | |
motorcycle=Motorbike.new(color: "black") | |
puts motorcycle.color=="black" | |
motorcycle.weave_through_traffic | |
puts motorcycle.status==:driving_like_a_crazy_person | |
motorcycle.brake | |
puts motorcycle.status==:stopped |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment