Skip to content

Instantly share code, notes, and snippets.

@sbswigart
Forked from dbc-challenges/P5: OO Inheritance.rb
Created August 19, 2013 06:17
Show Gist options
  • Save sbswigart/6266164 to your computer and use it in GitHub Desktop.
Save sbswigart/6266164 to your computer and use it in GitHub Desktop.
class Vehicle
attr_reader :color, :status, :wheels, :needs_gas_prob
def initialize(args={})
@color = args[:color] || "white"
@status = args[:status] || :stopped
@wheels = args[:wheels] || 4
@needs_gas_prob = args[:needs_gas_prob] || [true, false]
end
def drive
@status = :driving
end
def brake
@status = :stopped
end
def needs_gas?
@needs_gas_prob.sample
end
end
class Car < Vehicle
def initialize(args)
super(args)
end
end
class Bus < Vehicle
attr_reader :passengers
def initialize(args)
@num_seats = args[:num_seats]
@fare = args[:fare]
@passengers=[]
super(args)
end
def drive
return self.brake if stop_requested?
@status = :driving
end
def admit_passenger(passenger,money)
@passengers << passenger if money > @fare
end
def stop_requested?
return [true,false].sample
end
end
class Motorbike < Vehicle
def initialize(args)
super(args)
end
def drive
super
@speed = :fast
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
# puts "\nTesting Car"
# puts "###########"
# car = Car.new(color: 'red')
# p car.drive
# p car.brake
# p car.needs_gas?
# puts "\nTesting Bus"
# puts "###########"
# bus = Bus.new(color: 'blue', wheels: 4, num_seats: 20, fare: 2)
# p bus.drive
# p bus.admit_passenger(10000, 20)
# p bus.passengers
# p bus.admit_passenger("jil", 1)
# p bus.passengers
# p bus.brake
# p bus.stop_requested?
# p bus.needs_gas?
# puts "\nTesting Motorcycle"
# puts "###########"
# motorbike = Motorbike.new(color: 'purpule')
# p motorbike.drive
# p motorbike.brake
# p motorbike.needs_gas?
# p motorbike.weave_through_traffic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment