Home > Software engineering >  Why the difference between a loop and a single run
Why the difference between a loop and a single run

Time:12-27

enter image description here

[2,3,4,0,1].each do |value|
    print value if value==2...value==2
end

print true if 3==2...3==2

The loop prints 23401, but the last line does not print

I wonder what the problem is

I've tried a lot of loops, and I've found that conditions are always true once they're true

CodePudding user response:

In case of loop the command goes back to the point where the loop started and continues as long as the user desires while in case of single run it runs only ones

CodePudding user response:

in case of a loop the control goes back to the point where the loop starts till the point where it terminates while in the case of single run it runs only o

CodePudding user response:

I don't know why you use this expression as a condition value==2...value==2, it's a strange chose. It creates an instance of the Range class. A range is lazy, when you call it inside the block you literally get this, because the range acts as true.

[2,3,4,0,1].each do |value|
    print value if true
end

When you call print true if 3==2...3==2 it evaluates right away and becomes print true if false...false. And false...false is false.

  • Related