Skip to content

Instantly share code, notes, and snippets.

@jschmotzer
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 19, 2015 07:28
Show Gist options
  • Save jschmotzer/5918537 to your computer and use it in GitHub Desktop.
Save jschmotzer/5918537 to your computer and use it in GitHub Desktop.
class Vehicle
attr_reader :wheels, :color
def initialize(args)
@wheels = args[:wheels]
@color = args[:color]
end
def drive
@status = :driving
end
def brake
@brake = :stopped
end
def needs_gas?
return [true,true,false].sample
end
end
class Car < Vehicle
def initialize(args)
super
@wheels = 4
end
def needs_gas?
return [true,true,false].sample
end
end
class Bus < Vehicle
attr_accessor :num_seats, :passengers, :fare
def initialize(args)
super
@color = args[:color]
@wheels = args[:wheels]
@num_seats = args[:num_seats]
@fare = args[:fare]
@passengers=[]
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
def needs_gas?
return [true,true,false].sample
end
end
class Motorbike < Vehicle
def initialize(args)
super
@color = args[:color]
@wheels = 2
end
def drive
super
@speed = :fast
end
def needs_gas?
return [true,false].sample
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
audi = Car.new({:color => "black"})
puts audi.brake
puts audi.drive
puts audi.needs_gas?
school_bus = Bus.new({:color => "yellow", :wheels => 10, :num_seats => 30, :fare => 2, :passengers => 0})
puts school_bus.brake
puts school_bus.admit_passenger("john", 5)
puts school_bus.admit_passenger("rhinna", 4)
puts school_bus.needs_gas?
ducati = Motorbike.new({:color => "grey"})
puts ducati.drive
puts ducati.weave_through_traffic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment