Skip to content

Instantly share code, notes, and snippets.

@stevenspiel
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active January 4, 2016 02:09
Show Gist options
  • Save stevenspiel/8552876 to your computer and use it in GitHub Desktop.
Save stevenspiel/8552876 to your computer and use it in GitHub Desktop.
class Vehicle
def initialize(args)
@color = args[:color]
@wheels = 4
end
def drive
@status = :driving
end
def brake
@status = :stopped
end
def needs_gas?
return [true,true,false].sample
end
end
class Car < Vehicle; end
class Motorbike < Vehicle
def initialize(args)
super
@wheels = 2
end
def drive
super
@speed = :fast
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
class Bus < Vehicle
attr_reader :passengers
def initialize(args)
super
@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
def needs_gas?
return [true,true,true,false].sample
end
end
# Driver Code
car = Car.new(color: "red")
p car.drive
p car.brake
p car.needs_gas?
bus = Bus.new(color: "yellow", wheels: 8, num_seats: 48, fare: 0)
p bus.drive
bus.admit_passenger("Blake", 1)
p bus.passengers
p bus.stop_requested?
p bus.drive
p bus.needs_gas?
bike = Motorbike.new(color: "black")
p bike.drive
p bike.brake
p bike.needs_gas?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment