Skip to content

Instantly share code, notes, and snippets.

@jpark3000
Forked from dbc-challenges/P5: OO Inheritance.rb
Last active January 3, 2016 03:09
Show Gist options
  • Select an option

  • Save jpark3000/8400763 to your computer and use it in GitHub Desktop.

Select an option

Save jpark3000/8400763 to your computer and use it in GitHub Desktop.

Revisions

  1. jpark3000 revised this gist Jan 22, 2014. 2 changed files with 115 additions and 0 deletions.
    80 changes: 80 additions & 0 deletions inheritance.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    class Vehicle
    attr_reader :color, :wheels
    attr_accessor :status

    def initialize(args = {})
    @color = args[:color]
    @wheels = args[:wheels]
    @status = status
    end

    def drive
    self.status = :driving
    end

    def brake
    self.status = :stopped
    end
    end




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

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

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

    def drive
    self.brake if stop_requested?
    super
    end

    def admit_passenger(passenger, money)
    passengers << passenger if money >= fare
    end

    def stop_requested?
    [true,false].sample
    end

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


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

    def drive
    super
    @speed = :fast
    end

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

    def weave_through_traffic
    self.status = :driving_like_a_crazy_person
    end
    end
    35 changes: 35 additions & 0 deletions tests.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    require_relative 'inheritance'

    car = Car.new(color: 'red')
    bus = Bus.new(color: 'yellow', wheels: 18, num_seats: 32, fare: 2.25)
    motorbike = Motorbike.new(color: 'black')

    p car.color == 'red'
    p car.drive == :driving
    p car.brake == :stopped
    x = car.needs_gas?
    p x == true || x == false

    p bus.color == 'yellow'
    p bus.wheels == 18
    p bus.num_seats == 32
    p bus.fare == 2.25
    p bus.admit_passenger("john", 2.50).length == 1
    p bus.admit_passenger("bob", 1) == nil
    p bus.admit_passenger('megan', 2.25).length == 2
    p bus.passengers.length == 2
    p bus.brake == :stopped
    y = bus.stop_requested?
    p !!y == y
    foo = bus.needs_gas?
    p !!foo == foo

    p motorbike.color == 'black'
    p motorbike.drive == :fast
    p motorbike.status == :driving
    p motorbike.brake == :stopped
    z = motorbike.needs_gas?
    p !!z == z
    p motorbike.weave_through_traffic == :driving_like_a_crazy_person

    # p bus.drive
  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