Skip to content

Instantly share code, notes, and snippets.

@RyanHedges
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 24, 2015 06:09
Show Gist options
  • Save RyanHedges/6755599 to your computer and use it in GitHub Desktop.
Save RyanHedges/6755599 to your computer and use it in GitHub Desktop.
class Vehicle
attr_reader :color
def initialize(attributes)
@color = attributes[:color]
@wheels = attributes[:wheels]
@engine_status = :off
end
def drive
@stutus = :driving
end
def brake
@status = :stopped
end
def is_on?
if @engine_status == :on
return true
else
return false
end
end
def turn_on
@engine_status = :on
end
def turn_off
@engine_status = :off
end
end
class Car < Vehicle
def initialize(attributes = {})
super(attributes)
end
def needs_gas?
return [true,true,false].sample
end
def drive_slow
@status = :driving_super_slow
end
end
class Bus < Vehicle
attr_reader :passengers
def initialize(attributes = {})
super(attributes)
@num_seats = attributes[:num_seats]
@fare = attributes[: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
class Motorbike < Vehicle
def initialize(attributes = {})
super(attributes)
end
def drive
super
@speed = :fast
end
def needs_gas?
return [true,false,false,false].sample
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
test = Vehicle.new({:color => "red"})
p test.color == "red"
ducati = Motorbike.new({:color => "yellow", :wheels => 2})
megabus = Bus.new({:color => "green", :wheels => 6, :num_seats => 125, :fare => 125.00})
ford = Car.new({:color => "red", :wheels => 4})
p ducati.drive == :fast
p ducati.brake == :stopped
p ducati.weave_through_traffic == :driving_like_a_crazy_person
p ford.drive == :driving
p ford.brake == :stopped
p ford.needs_gas? == false || true
p megabus.brake == :stopped
p megabus.admit_passenger("Ryan", 150) == megabus.passengers
p megabus.admit_passenger("Katie", 150) == megabus.passengers
p megabus.stop_requested? == false || true
p megabus.brake == :stopped
p ducati.is_on? == false
p ducati.turn_on == :on
p ducati.is_on? == true
p ford.is_on? == false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment