Skip to content

Instantly share code, notes, and snippets.

@stomatocode
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 19, 2015 06:59
Show Gist options
  • Save stomatocode/5915239 to your computer and use it in GitHub Desktop.
Save stomatocode/5915239 to your computer and use it in GitHub Desktop.
class Vehicle
attr_reader :color, :speed, :status
def initialize(args)
@color = args[:color]
@speed = :slow
@status = :stopped
end
def drive
@status = :driving
end
def brake
@status = :stopped
end
def needs_gas?
return [true,true,false].sample
end
end
class Car < Vehicle
@@WHEELS = 4
attr_reader :engine, :body_type
def initialize(args)
super
@engine = args[:engine]
@body_type = args[:body_type]
end
def speed
@status = :fast
end
def rev_engine
"VROOM VROOM"
end
end
class Bus < Vehicle
@@WHEELS = 6
attr_reader :passengers
def initialize(args)
super
@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)
money >= @fare ? @passengers << passenger : "Get off the bus!"
end
def stop_requested?
return [true,false].sample
end
end
class Motorbike < Vehicle
@@WHEELS = 2
def speed
@speed = :fast
end
def weave_through_traffic
@status = :driving_like_a_crazy_person
end
end
################################################
commuter = Bus.new(num_seats: 20, fare: 2)
p commuter.stop_requested?
p commuter.admit_passenger("Jake", 2)
honda = Motorbike.new(color: "red")
p honda.color
p honda.speed
p honda.status
p honda.weave_through_traffic
maserati = Car.new(engine: "V8",body_type: "Coupe",color: "black",)
puts "maserati below"
p maserati.status
p maserati.speed
p maserati.drive
p maserati.brake
p maserati.status
p maserati.engine
p maserati.body_type
p maserati.rev_engine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment