Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 19, 2015 10:29
-
-
Save jesseclay/5941320 to your computer and use it in GitHub Desktop.
Revisions
-
jesseclay revised this gist
Jul 6, 2013 . 1 changed file with 0 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -123,14 +123,6 @@ def needs_gas? puts muni.color == "Green" puts muni.status == :stopped muni.drive # p muni.admit_passenger("Tom Jones", 2.50) class Motorbike < Vehicle -
jesseclay revised this gist
Jul 6, 2013 . 1 changed file with 207 additions and 64 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,70 +1,213 @@ class Vehicle attr_reader :color, :status def initialize(args) @wheels = self.wheels @seats = self.seats @status = self.status @color = args.fetch(:color) {self.color} @speed = self.speed end def seats raise "No default number of seats for Vehicle class" end def wheels 4 end def color return @color if @color != nil raise "No default color for Vehicle class" end def status return @status if @status != nil :stopped end def speed return @speed if @speed != nil raise "No default speed for vehicle class" end def drive @status = :driving end def brake @status = :stopped end end class Car < Vehicle def seats 5 end def color "grey" end def speed :moderate end def needs_gas return [true,true,false].sample end end car = Car.new({}) puts car.color == "grey" puts car.status == :stopped puts car.wheels == 4 puts car.seats == 5 car.drive puts car.status == :driving class Bus < Vehicle attr_reader :passengers def initialize(args) super(args) @passengers = args.fetch(:passengers) {[]} @fare = args.fetch(:fare) {raise "No fare amount provided"} end def speed :slow end def wheels 6 end def color return @color if @color != nil "Red and white" end def seats 52 end def drive return self.brake if stop_requested? @status = :driving end def admit_passenger(passenger,money) @passengers << passenger if money > @fare end def stop_requested? return [true,false].sample end def needs_gas? return [true,true,true,false].sample end end muni = Bus.new({:color => "Green", :fare => 2.00}) puts muni.wheels == 6 puts muni.seats == 52 puts muni.color == "Green" puts muni.status == :stopped muni.drive # puts muni.status == :driving # ducati.weave_through_traffic # puts ducati.status == :driving_like_a_crazy_person # puts muni. # p muni.drive #== :driving # p muni.admit_passenger("Tom Jones", 2.50) class Motorbike < Vehicle def speed :fast end def wheels 2 end def seats 1 end def drive @status = :driving @speed = :fast end def needs_gas? return [true,false,false,false].sample end def weave_through_traffic @status = :driving_like_a_crazy_person end end puts "Motorcycle: " ducati = Motorbike.new({:color => "fire-red"}) puts ducati.wheels == 2 puts ducati.seats == 1 puts ducati.color == "fire-red" puts ducati.status == :stopped ducati.drive puts ducati.status == :driving ducati.weave_through_traffic puts ducati.status == :driving_like_a_crazy_person # require 'minitest/spec' # require 'minitest/autorun' # describe Car do # before :each do # @car = Car.new({:color => 'red'}) # end # describe "#new" do # it "takes one parameter and returns a Car object" do # @car.must_be_instance_of Car # end # end # describe "#drive" do # it "returns :driving" do # @car.drive.must_equal :driving # end # end # describe "#break" do # it "returns :stopped" do # @car.brake.must_equal :stopped # end # end # describe "#needs_gas" do # it "returns true or false" do # # @car.needs_gas.must_equal true || false # end # end # end -
dbc-challenges renamed this gist
Jun 25, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
dbc-challenges created this gist
Jun 11, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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