Main Content
Alternating valuses / flip-flop / toggle - example in Ruby
Archive - Originally posted on "The Horse's Mouth" - 2016-05-17 05:34:03 - Graham Ellis
If you want to alternate the value of a variable, here's two ways you can do it in Ruby - answers to a question during this week's Ruby Course
# Too clever?
t = 0
(1..20).each do
puts (((t=(t+1)%2)==0) ? "yes" : "no")
end
#Â better?
t = 0
(1..20).each do
t += 1
t %= 2
puts ((t==0) ? "YES" : "NO")
end
Source code - [here].