Created
November 16, 2013 20:45
-
-
Save gouf/7505047 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
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 |
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
# 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