Flip Flop
The flip flop operator (flipfloperator) is a special form of conditional syntax. The syntax looks like the range feature but is distinct syntax that behaves differently.
Not what you're looking for? You may be looking for the range syntax.
The flip flop expression (cond1 .. cond2
) evaluates to false by default. When the condition on the left side of the flip flop becomes true, then the flip flop expression evaluates to true. As execution proceeds, the expression will continue to evaluate to true until a change occurs that causes the right-side condition to evalute to true. Once the condition on the right side becomes true then the flip flop expression evaluates to false.
The flip flop expression can be thought of as defining a window of execution. You do nothing until you reach the beginning of the window. Once you've reached the left edge of the window, you do something. You keep going, doing that thing, until you've reached the right edge of the window. Your outer loop may continue at this point, but you're no longer in the window of execution.
Here is an example from the Ruby docs:
> selected = []
=> []
0.upto 10 do |value|
selected << value if value==2..value==8
end
=> 0
> selected
=> [2, 3, 4, 5, 6, 7, 8]
Once the value is 2
, the left side of the flip flop is true and values are shoveled into the array. Iteration continues and when the value is 8
, the right side of the flip flop becomes true making the condition evaluate to false
. No more values are shoveled into the array.
There is also an exclusive range version that uses ...
.