Skip to content

Instantly share code, notes, and snippets.

@jsedwards
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active December 24, 2015 05:19
Show Gist options
  • Save jsedwards/6749721 to your computer and use it in GitHub Desktop.
Save jsedwards/6749721 to your computer and use it in GitHub Desktop.

Revisions

  1. Jeff Edwards revised this gist Sep 29, 2013. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion P5: OO Inheritance.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    class Vehicle
    attr_reader :color
    def initialize(args)
    @color = args[:color]
    end
    @@ -8,6 +9,9 @@ def brake
    def drive
    @status = :driving
    end
    def ok_to_text?
    @status != :driving
    end
    end

    class Car < Vehicle
    @@ -85,5 +89,11 @@ def assert(expression, message)
    assert(harley = Motorbike.new(color: "black"), "new bikes can be created")
    assert(harley.drive == :fast, "bikes can drive, they go fast")
    assert(harley.weave_through_traffic == :driving_like_a_crazy_person, "bikes weave through traffic like a crazy person")

    #part 3 tests
    assert(harley.color == "black", "bikes can have colors")
    assert(red_rocket.color == "red", "bikes can have colors")
    assert(big_bird.color == "yellow", "bikes can have colors")
    assert(red_rocket.ok_to_text? == true, "ok for car to text when stopped")
    red_rocket.drive
    assert(red_rocket.ok_to_text? == false, "not ok for car to text when driving")

  2. Jeff Edwards revised this gist Sep 29, 2013. 1 changed file with 49 additions and 30 deletions.
    79 changes: 49 additions & 30 deletions P5: OO Inheritance.rb
    Original file line number Diff line number Diff line change
    @@ -1,39 +1,45 @@
    class Car
    @@WHEELS = 4
    def initialize(args)
    @color = args[:color]
    @wheels = @@WHEELS
    end
    def drive
    @status = :driving
    end
    class Vehicle
    def initialize(args)
    @color = args[:color]
    end
    def brake
    @status = :stopped
    @status = :stopped
    end
    def needs_gas?
    def drive
    @status = :driving
    end
    end

    class Car < Vehicle
    @@WHEELS = 4
    def initialize(args)
    super
    @wheels = args[:wheels]
    end

    def needs_gas?
    return [true,true,false].sample
    end
    end

    end
    class Bus

    class Bus < Vehicle
    attr_reader :passengers
    def initialize(args)
    @color = args[:color]
    @wheels = args[:wheels]
    super
    @wheels = args[:wheels]
    @num_seats = args[:num_seats]
    @fare = args[:fare]
    @passengers=[]
    end
    def drive
    return self.brake if stop_requested?
    @status = :driving
    super
    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
    @@ -45,26 +51,39 @@ def needs_gas?
    end


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

    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
    end


    #TESTS
    def assert(expression, message)
    raise message unless expression
    end

    assert(red_rocket = Car.new({color: "red"}), "new car can be created")
    assert(red_rocket.drive, "cars can drive")
    assert(red_rocket.brake, "cars can brake")
    #assert(!!red_rocket.needs_gas? == red_rocket.needs_gas?, " weird test cars either need gas or they don't")
    assert(big_bird = Bus.new(color: "yellow", fare: 2, num_seats: 48, wheels: 10), "new bus can be created")
    assert(big_bird.passengers == [], "new buses don't have any passengers")
    assert(big_bird.drive == :stopped || :driving, "buses drive")
    assert(big_bird.admit_passenger("Joe",5), "buses admit passengers with more money than the fare")
    assert(big_bird.admit_passenger("Cheap Skate",1)==nil, "buses dont' admit passengers with less money than the fare")
    assert(harley = Motorbike.new(color: "black"), "new bikes can be created")
    assert(harley.drive == :fast, "bikes can drive, they go fast")
    assert(harley.weave_through_traffic == :driving_like_a_crazy_person, "bikes weave through traffic like a crazy person")


  3. @dbc-challenges dbc-challenges renamed this gist Jun 25, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @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