Created
October 21, 2009 01:57
-
-
Save vroy/214779 to your computer and use it in GitHub Desktop.
This file contains 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 TrafficLight | |
def initialize | |
@colors = ["green", "yellow", "red"] | |
@index = 0 | |
end | |
def color | |
@colors[@index] | |
end | |
def cycle! | |
@index = (@index + 1) % @colors.size | |
end | |
end | |
light = TrafficLight.new | |
light.color # => "green" | |
light.cycle! | |
light.color # => "yellow" | |
light.cycle! | |
light.color # => "red" | |
light.cycle! | |
light.color # => "green" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment