Skip to content

Instantly share code, notes, and snippets.

@Joe11000
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 19, 2015 07:29
Show Gist options
  • Save Joe11000/5919524 to your computer and use it in GitHub Desktop.
Save Joe11000/5919524 to your computer and use it in GitHub Desktop.
class Vehicle
attr_reader :color, :wheels
def initialize(vehicle_attributes_hash)
@status = vehicle_attributes_hash[:status]
@wheels = vehicle_attributes_hash[:wheels]
end
def drive
@status = :driving
end
def brake
@status = :stopped
end
def needs_gas?(arr_of_liklihood)
arr_of_liklihood.sample
end
end
class Car < Vehicle
@@WHEELS = 4
def initialize(vehicle_attributes_hash)
vehicle_attributes_hash[:wheels] = 4
@color = args[:color]
@wheels = @@WHEELS
end
def needs_gas?
super([true,true,false])
end
end
class Bus < Vehicle
attr_reader :passengers
def initialize(vehicle_attributes_hash)
super(vehicle_attributes_hash)
@num_seats = args[:num_seats]
@fare = args[: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]
end
def needs_gas?
super([true,true,true,false])
end
end
class Motorbike < Vehicle
@@WHEELS = 2
def initialize(vehicle_attributes_hash)
super(vehicle_attributes_hash)
@wheels = @@WHEELS
end
def drive
@speed = :fast
super
end
def needs_gas?
super([true,false,false,false])
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
vehicle_features = {:color => "blue", :wheels => 4, :num_seats =>4, :fare => 0, :speed=>"fast"}
car = Car.new(vehicle_features)
puts car.drive == :driving
puts car.brake == :stopped
puts car.needs_gas? == true || car.needs_gas? == false
puts
bus = Bus.new(vehicle_features)
puts bus.drive == :driving
puts bus.brake == :stopped
puts (bus.needs_gas? == true || bus.needs_gas? == false)
puts (bus.stop_requested? == true || bus.stop_requested? == false)
puts bus.admit_passenger("Joe", 56)
puts
bike = Motorbike.new(vehicle_features)
puts bike.drive == :driving
puts bike.brake == :stopped
puts (bike.needs_gas? == true || bike.needs_gas? == false)
puts (bike.weave_through_traffic == :driving_like_a_crazy_person)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment