Skip to content

Instantly share code, notes, and snippets.

@Birdrock
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active August 29, 2015 13:56
Show Gist options
  • Save Birdrock/8927815 to your computer and use it in GitHub Desktop.
Save Birdrock/8927815 to your computer and use it in GitHub Desktop.
class Vehicle
attr_writer :status
def initialize(args)
@color = args[:color]
end
def brake
self.status = :stopped
end
def engine_malfunction
self.status = :broken
end
def hire_mechanic
self.status = :stopped
end
end
class Car < Vehicle
@@WHEELS = 4
def initialize(args)
super(args)
# @color = args[:color]
@wheels = @@WHEELS
end
def drive
self.status = :driving
end
def needs_gas?
return [true,true,false].sample
end
def roll_down_windows
"Ah; a cool breeze"
end
end
class Bus < Vehicle
attr_reader :passengers
def initialize(args)
super(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?
self.status = :driving
end
def admit_passenger(passenger,money)
self.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
class Motorbike < Vehicle
@@WHEELS = 2
def initialize(args)
super(args)
# @color = args[:color]
@wheels = @@WHEELS
end
def drive
self.status = :driving
@speed = :fast
end
def needs_gas?
return [true,false,false,false].sample
end
def weave_through_traffic
self.status = :driving_like_a_crazy_person
end
def do_wheelie
"DOING A SWEET WHEELIE"
end
end
p car = Car.new(color: 'red')
p car.drive
p car.brake
p car.roll_down_windows
p bus = Bus.new(color: 'blue', wheels: 8, num_seats: 20, fare: 5)
p bus.admit_passenger('Bob',4)
p bus.admit_passenger('Joe',6)
p bus.needs_gas?
p bus.stop_requested?
# p bus.stop_requested?
p bus.drive
p moto = Motorbike.new(color: 'black')
p moto.drive
p moto.brake
p moto.needs_gas?
p moto.weave_through_traffic
p moto.do_wheelie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment