Skip to content

Instantly share code, notes, and snippets.

@gouf
Created November 16, 2013 20:45
Show Gist options
  • Save gouf/7505047 to your computer and use it in GitHub Desktop.
Save gouf/7505047 to your computer and use it in GitHub Desktop.
state: A
toggled: B
toggled: A
toggled: B
--------------
state: A
cycle: B
cycle: C
cycle: D
cycle: E
cycle: A
cycle: B
cycle: C
cycle: D
# See: smart javascript state toggle
# https://coderwall.com/p/ufl1wa
class State
attr_reader :state
def initialize
@toggle = {'A' => 'B', 'B' => 'A'}
@state = 'A'
end
def toggle
@state = @toggle[@state]
end
end
class Cycle
attr_reader :state
def initialize
@cycle = %w(A B C D E)
@state = 'A'
end
def cycle
@state = (@cycle = @cycle.rotate).first
end
end
s = State.new
puts "state: #{s.state}"
3.times do
puts "toggled: #{s.toggle}"
end
puts '--------------'
c = Cycle.new
puts "state: #{c.state}"
8.times do
puts "cycle: #{c.cycle}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment