Created
July 24, 2013 09:49
-
-
Save leoaretakis/6069293 to your computer and use it in GitHub Desktop.
Exercise proposed by Leandro
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 characters
| class Car | |
| attr_accessor :color | |
| attr_accessor :model | |
| def engage_gear(gear) | |
| @gear = 1 | |
| @gear = gear if gear | |
| end | |
| def accelerate | |
| @status = "stopped" | |
| @status = 'moving' if @gear > 0 | |
| end | |
| def reduce | |
| engage_gear(@gear - 1) if @gear > 0 | |
| end | |
| def stop | |
| reduce while @gear > 0 | |
| @status = "stopped" | |
| end | |
| def start | |
| engage_gear(nil) | |
| accelerate | |
| end | |
| def in_gear? | |
| @gear > 0 | |
| end | |
| def stopped? | |
| @status == 'stopped' | |
| end | |
| def in_movement? | |
| @status == "moving" | |
| end | |
| end | |
| cars = Array.new | |
| (1..10).each { |n| cars << Car.new } | |
| puts "Total of cars is #{cars.length}!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment