Enumerable#cycle
offers an easy way to either repeat a certain pattern n times or just to switch between two predefined states. irb> array = [1, 2, 3] => [1, 2, 3] irb> array.cycle(3).to_a => [1, 2, 3, 1, 2, 3, 1, 2, 3]
irb> button = ['on', 'off'].cycle => # irb> button.next => "on" irb> button.next => "off"