[DOC] Refine flip-flop
This commit is contained in:
parent
8dc357fa94
commit
e6c1db1d8a
@ -513,9 +513,9 @@ and modifier +if+ has lower precedence than <code>=</code>.
|
|||||||
|
|
||||||
== Flip-Flop
|
== Flip-Flop
|
||||||
|
|
||||||
The flip-flop is a rarely seen conditional expression. It's primary use is
|
The flip-flop is a slightly special conditional expression. One of its
|
||||||
for processing text from ruby one-line programs used with <code>ruby -n</code>
|
typical uses is processing text from ruby one-line programs used with
|
||||||
or <code>ruby -p</code>.
|
<code>ruby -n</code> or <code>ruby -p</code>.
|
||||||
|
|
||||||
The form of the flip-flop is an expression that indicates when the
|
The form of the flip-flop is an expression that indicates when the
|
||||||
flip-flop turns on, <code>..</code> (or <code>...</code>), then an expression
|
flip-flop turns on, <code>..</code> (or <code>...</code>), then an expression
|
||||||
@ -524,7 +524,6 @@ will continue to evaluate to +true+, and +false+ when off.
|
|||||||
|
|
||||||
Here is an example:
|
Here is an example:
|
||||||
|
|
||||||
|
|
||||||
selected = []
|
selected = []
|
||||||
|
|
||||||
0.upto 10 do |value|
|
0.upto 10 do |value|
|
||||||
@ -533,15 +532,16 @@ Here is an example:
|
|||||||
|
|
||||||
p selected # prints [2, 3, 4, 5, 6, 7, 8]
|
p selected # prints [2, 3, 4, 5, 6, 7, 8]
|
||||||
|
|
||||||
In the above example, the on condition is <code>n==2</code>. The flip-flop
|
In the above example, the `on' condition is <code>n==2</code>. The flip-flop
|
||||||
is initially off (false) for 0 and 1, but becomes on (true) for 2 and remains
|
is initially `off' (false) for 0 and 1, but becomes `on' (true) for 2 and
|
||||||
on through 8. After 8 it turns off and remains off for 9 and 10.
|
remains `on' through 8. After 8 it turns off and remains `off' for 9 and 10.
|
||||||
|
|
||||||
The flip-flop must be used inside a conditional such as +if+, +while+,
|
The flip-flop must be used inside a conditional such as <code>!</code>,
|
||||||
+unless+, +until+ etc. including the modifier forms.
|
<code>? :</code>, +not+, +if+, +while+, +unless+, +until+ etc. including the
|
||||||
|
modifier forms.
|
||||||
|
|
||||||
When you use an inclusive range (<code>..</code>), the off condition is
|
When you use an inclusive range (<code>..</code>), the `off' condition is
|
||||||
evaluated when the on condition changes:
|
evaluated when the `on' condition changes:
|
||||||
|
|
||||||
selected = []
|
selected = []
|
||||||
|
|
||||||
@ -555,7 +555,7 @@ Here, both sides of the flip-flop are evaluated so the flip-flop turns on and
|
|||||||
off only when +value+ equals 2. Since the flip-flop turned on in the
|
off only when +value+ equals 2. Since the flip-flop turned on in the
|
||||||
iteration it returns true.
|
iteration it returns true.
|
||||||
|
|
||||||
When you use an exclusive range (<code>...</code>), the off condition is
|
When you use an exclusive range (<code>...</code>), the `off' condition is
|
||||||
evaluated on the following iteration:
|
evaluated on the following iteration:
|
||||||
|
|
||||||
selected = []
|
selected = []
|
||||||
@ -567,5 +567,5 @@ evaluated on the following iteration:
|
|||||||
p selected # prints [2, 3, 4, 5]
|
p selected # prints [2, 3, 4, 5]
|
||||||
|
|
||||||
Here, the flip-flop turns on when +value+ equals 2, but doesn't turn off on the
|
Here, the flip-flop turns on when +value+ equals 2, but doesn't turn off on the
|
||||||
same iteration. The off condition isn't evaluated until the following
|
same iteration. The `off' condition isn't evaluated until the following
|
||||||
iteration and +value+ will never be two again.
|
iteration and +value+ will never be two again.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user