Uncle bearded need help on the trigger.

In the book "Ruby Programming Language" authors Matsumoto, Flanagan have the code:

(1..10).each {|х| print х if х==3..х==5 } 

The output of the following 345 goes further nonsense:

A trigger consists of two Boolean expressions united by an operator .., in the context of a condition or loop. The trigger expression is evaluated to false as long as the left expression evaluates to true (maybe it should be false).

As soon as this expression becomes true , the expression "flips" to the steady state true . It will remain in this state, and subsequent calculations will return true until the right expression evaluates to true (it may have to until the right expression evaluates to true).

When this happens, the trigger is "thrown" back to the steady state false . Subsequent evaluations of the expression return false until the left expression is again true .

About the trigger with 3 points translation is even worse.

The difference is that when using the operator .. when the trigger is thrown to true, it returns true, but it also tests its right expression to see if it should not relocate its internal state back to false. When using the ... operator, the right expression is first tested.


 (1..10).each {|х| print х if х==3..х==5 } 
  1. left expression 1, 2 == 3 ( false ) state: false
  2. the left expression 3 == 3 ( true ) changes the state to: true returns true , the right side is checked ( false ).
  3. left expression 4 == 3 ( false ) state remains: true as right expression 4 == 5 false
  4. the left expression 5 == 3 ( false ) the state remains: true, the test of the right expression 5 == 5 ( true ) is reset to the state false .
  5. 6,7,8,9,10 == 3 ( false ) state does not change.

    Well, with ... dots, I'm confused. If that is not strictly judged, please understand and forgive.

  • Oh yes, flip-flops. The feature of the language, for the use of which any of your ruby ​​colleague will be ready to do something very bad with you, and which they still can’t throw out of Ruby to the hell ...%) - D-side

1 answer 1

 if (condition1)..(condition2) # do something end 

The idea is that if the first condition is true, then the invisible lever switches, and from that moment the condition will be fulfilled until the second condition also becomes true. Example:

 (1..20).each do |i| puts i if (i == 3)..(i == 15) end 

It prints all numbers from 3 to 15, but if 15 is skipped in a cycle, it will continue to print.

  • And so (1..20) .each do | i | puts i if (i == 3) ... (i == 15) end how the check goes - Vyacheslav
  • for this example, the result is the same. but triggers are switched differently and in the case of difficult conditions the result may differ - codename0082016
  • The trigger is not what your condition is equal to, and whether the state was switched from one to another - therefore, it is correct in the original - codename0082016
  • the trigger is not what your condition is equal to, and whether the state has switched from one to another. This is just what this sentence lacks in translation. Thank. - Vyacheslav
  • in the second paragraph, you seem to be right (может надо было пока правое выражение не вычислиться в true). - codename0082016