Forked from dbc-challenges/P5: OO Inheritance.rb
Last active
December 19, 2015 07:28
-
-
Save abrahamsangha/5918548 to your computer and use it in GitHub Desktop.
Revisions
-
abrahamsangha revised this gist
Jul 4, 2013 . 1 changed file with 47 additions and 67 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 @@ -14,88 +14,66 @@ def needs_gas? return @gas_mileage.sample end def stunt #3) get creative, add functionality to vehicle class, and subclass @status = :cruising end end class Car < Vehicle def initialize(args) super(args) @wheels = 4 @gas_mileage = [true,true,false] end end class Bus < Vehicle attr_reader :passengers def initialize(args) super @num_seats = args[:num_seats] @fare = args[:fare] @passengers=[] @gas_mileage = [true,true,true,false] 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 def stunt return "You can't stunt in a bus" end end class Motorbike < Vehicle def initialize(args) super @wheels = 2 @gas_mileage = [true,false,false,false] end def drive super @speed = :fast end def weave_through_traffic @status = :driving_like_a_crazy_person end end p volvo = Car.new({color: :red}) @@ -120,3 +98,5 @@ def weave_through_traffic p ninja.brake p ninja.needs_gas? p ninja.weave_through_traffic p megabus.stunt p volvo.stunt -
dbc-apprentice revised this gist
Jul 3, 2013 . 1 changed file with 88 additions and 36 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,122 @@ class Vehicle def initialize(args) @color = args[:color] @wheels = args[:wheels] @gas_mileage = [true,false] end def drive @status = :driving end def brake @status = :stopped end def needs_gas? return @gas_mileage.sample end def stunt #3) get creative, add functionality to vehicle class, and subclass end end class Car < Vehicle # @@WHEELS = 4 def initialize(args) # @color = args[:color] super(args) @wheels = 4 #@@WHEELS @gas_mileage = [true,true,false] end # def drive # @status = :driving # end # def brake # @status = :stopped # end # def needs_gas? # return [true,true,false].sample # end end class Bus < Vehicle attr_reader :passengers def initialize(args) super # @color = args[:color] # @wheels = args[:wheels] @num_seats = args[:num_seats] @fare = args[:fare] @passengers=[] @gas_mileage = [true,true,true,false] 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 # def needs_gas? # return [true,true,true,false].sample # end end class Motorbike < Vehicle # @@WHEELS = 2 def initialize(args) # @color = args[:color] super @wheels = 2 @gas_mileage = [true,false,false,false] 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 p volvo = Car.new({color: :red}) p volvo.drive p volvo.brake p volvo.needs_gas? p '*******' p megabus = Bus.new({color: :purple, wheels: 12, num_seats: 60, fare: 25}) p megabus.drive p megabus.admit_passenger("Ghostface", 30) p megabus.brake p megabus.stop_requested? p megabus.needs_gas? p megabus.drive p megabus.drive p megabus.drive p megabus.drive p megabus.drive p '*******' p ninja = Motorbike.new({color: :black}) p ninja.drive p ninja.brake p ninja.needs_gas? p ninja.weave_through_traffic -
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