Skip to content

Instantly share code, notes, and snippets.

@jjoseph05
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 19, 2015 07:29
Show Gist options
  • Save jjoseph05/5918642 to your computer and use it in GitHub Desktop.
Save jjoseph05/5918642 to your computer and use it in GitHub Desktop.
class Vehicle
attr_reader :wheels
attr_accessor :color
@@Wheels = 4
def initialize(args)
@color = args[:color]
@wheels = @@Wheels
end
def drive
@status = :driving
end
def brake
@status = :stopped
end
def needs_gas?
return [true,false,false,false].sample
end
end
class Car < Vehicle
end
class Bus < Vehicle
attr_reader :passengers
attr_writer :wheels
def initialize(args)
super
@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
end
class Motorbike < Vehicle
@@Wheels = 2
def drive
# @status = :driving
@speed = :fast
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
#===========TEST CODE============
p '-'*100
puts ducati = Motorbike.new(:color => "Orange")
p ducati.drive == :fast
p ducati.brake == :stopped
p ducati.weave_through_traffic == :driving_like_a_crazy_person
p ducati.wheels == 2
p '-'*100
p magicbus = Bus.new(:color => "Yellow" , :fare => 2 , :wheels => 14)
p magicbus.admit_passenger('bill' , 5)
p magicbus.passengers.length == 1
p magicbus.wheels == 14
p magicbus.color == 'Yellow'
p '-'*100
p kit = Car.new(:color => "Black")
p kit.wheels == 4
p kit.color == 'Black'
p kit.drive == :driving
p kit.brake == :stopped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment