Skip to content

Instantly share code, notes, and snippets.

@jbwilmoth
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active August 29, 2015 13:56
Show Gist options
  • Save jbwilmoth/8928613 to your computer and use it in GitHub Desktop.
Save jbwilmoth/8928613 to your computer and use it in GitHub Desktop.
class Vehicle
def initialize(args)
@color = args[:color]
@num_seats = args[:num_seats]
end
def drive
@status = :driving
end
def brake
@status = :stopped
end
def needs_gas?
return [true,false,true,false].sample
end
end
class Car < Vehicle
@@WHEELS = 4
def self.wheels
@@WHEELS
end
end
class Bus < Vehicle
attr_reader :passengers
def initialize(args)
super
@fare = args[:fare]
@passengers=[]
end
def drive
return self.brake if stop_requested?
end
def admit_passenger(passenger,money)
@passengers << passenger if money >= @fare
end
def stop_requested?
return [true,false].sample
end
end
class Motorbike < Vehicle
@@WHEELS = 2
def self.wheels
@@WHEELS
end
def drive
@speed = :fast
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
greyhound = Bus.new(:fare => 2, :color => "red", :num_seats => 20)
p greyhound
puts greyhound.admit_passenger(3, 6)
puts greyhound.drive
puts greyhound.stop_requested?
puts greyhound.drive
puts greyhound.needs_gas?
moto = Motorbike.new(:color => "gold", :num_seats => 1)
puts moto.drive
puts moto.weave_through_traffic
supercar = Car.new(:color => "black", :num_seats => 2)
puts supercar.drive
puts supercar.brake
puts supercar.needs_gas?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment