Skip to content

Instantly share code, notes, and snippets.

@vroy
Created October 21, 2009 01:57
Show Gist options
  • Save vroy/214779 to your computer and use it in GitHub Desktop.
Save vroy/214779 to your computer and use it in GitHub Desktop.
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