Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 24, 2015 05:19
-
-
Save jsedwards/6749721 to your computer and use it in GitHub Desktop.
Revisions
-
Jeff Edwards revised this gist
Sep 29, 2013 . 1 changed file with 11 additions and 1 deletion.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,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") -
Jeff Edwards revised this gist
Sep 29, 2013 . 1 changed file with 49 additions and 30 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,39 +1,45 @@ class Vehicle def initialize(args) @color = args[:color] end def brake @status = :stopped end 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 class Bus < Vehicle attr_reader :passengers def initialize(args) super @wheels = args[:wheels] @num_seats = args[:num_seats] @fare = args[:fare] @passengers=[] end 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 @@ -45,26 +51,39 @@ def needs_gas? end class Motorbike < Vehicle @@WHEELS = 2 def drive super @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 #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") -
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