Skip to content

Instantly share code, notes, and snippets.

@clay-whitley
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 20, 2015 03:49
Show Gist options
  • Select an option

  • Save clay-whitley/6066247 to your computer and use it in GitHub Desktop.

Select an option

Save clay-whitley/6066247 to your computer and use it in GitHub Desktop.

Revisions

  1. clay-whitley revised this gist Jul 24, 2013. 1 changed file with 99 additions and 59 deletions.
    158 changes: 99 additions & 59 deletions P5: OO Inheritance.rb
    Original file line number Diff line number Diff line change
    @@ -1,70 +1,110 @@
    class Car
    @@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,true,false].sample
    end

    class Vehicle
    def initialize(args)
    @status = :stopped
    @color = args[:color]
    @gas_capacity = 10
    @gas = 5
    end

    def drive
    unless needs_gas?
    @gas -= 1
    @status = :driving
    else
    return "You're out of gas!"
    end
    end

    def brake
    @status = :stopped
    end

    def needs_gas?
    @gas == 0 ? true : false
    end

    def fill_tank
    @gas = @gas_capacity
    end
    end
    class Bus
    attr_reader :passengers
    def initialize(args)
    @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 brake
    @status = :stopped
    end
    def stop_requested?
    return [true,false].sample
    end
    def needs_gas?
    return [true,true,true,false].sample
    end


    class Car < Vehicle
    def initialize(args)
    super
    @wheels = 4
    end
    end

    class Bus < Vehicle
    attr_reader :passengers
    def initialize(args)
    super
    @wheels = 8
    @num_seats = args[:num_seats]
    @fare = args[:fare]
    @passengers=[]
    @gas_capacity = 30
    end

    class Motorbike
    @@WHEELS = 2
    def initialize(args)
    @color = args[:color]
    @wheels = @@WHEELS
    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
    end

    end

    class Motorbike < Vehicle
    def initialize(args)
    super
    @wheels = 2
    end

    def drive
    @status = :driving
    super
    @speed = :fast
    end

    def brake
    @status = :stopped
    end
    def needs_gas?
    return [true,false,false,false].sample
    end
    def weave_through_traffic
    @status = :driving_like_a_crazy_person
    end
    end
    def weave_through_traffic
    @status = :driving_like_a_crazy_person
    end
    end

    car_instance = Car.new color: "Red"
    bus_instance = Bus.new color: "White", num_seats: 20, fare: 3
    bike_instance = Motorbike.new color: "Black"


    puts "Car tests:"
    p car_instance.drive == :driving
    p car_instance.brake == :stopped
    gas_status = car_instance.needs_gas?
    p gas_status == true || gas_status == false


    puts "Bus tests:"
    p bus_instance.passengers == []
    bus_status = bus_instance.drive
    p bus_status == :driving || bus_status == :stopped
    p bus_instance.admit_passenger("rich", 5) == ["rich"]
    p bus_instance.passengers == ["rich"]
    p bus_instance.admit_passenger("poor", 2).nil?
    p bus_instance.brake == :stopped
    stop_status = bus_instance.stop_requested?
    p stop_status == true || stop_status == false
    gas_status = bus_instance.needs_gas?
    p gas_status == true || gas_status == false

    puts "Bike tests:"
    p bike_instance.drive == :fast
    p bike_instance.brake == :stopped
    gas_status = bike_instance.needs_gas?
    p gas_status == false || gas_status == true
    p bike_instance.weave_through_traffic == :driving_like_a_crazy_person
  2. @dbc-challenges dbc-challenges renamed this gist Jun 25, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @dbc-challenges dbc-challenges created this gist Jun 11, 2013.
    70 changes: 70 additions & 0 deletions vehicle.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    class Car
    @@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,true,false].sample
    end

    end
    class Bus
    attr_reader :passengers
    def initialize(args)
    @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 brake
    @status = :stopped
    end
    def stop_requested?
    return [true,false].sample
    end
    def needs_gas?
    return [true,true,true,false].sample
    end


    end


    class Motorbike
    @@WHEELS = 2
    def initialize(args)
    @color = args[:color]
    @wheels = @@WHEELS

    end

    def drive
    @status = :driving
    @speed = :fast
    end

    def brake
    @status = :stopped
    end
    def needs_gas?
    return [true,false,false,false].sample
    end
    def weave_through_traffic
    @status = :driving_like_a_crazy_person
    end
    end