The following code is using a flip-flop operator.
(1..10).each {|x| print "#{x}," if x==3..x==5 }
Why are the results 3,4,5
?
I think it should be 3,4
.
As mentioned in a tutorial, this expression becomes true when x == 3
, and continues to be true until x == 5
. How could '5' been printed if it evaluates to false? Could anyone please clarify that for me?
Are you looking for an exclusive range? You can use three dots and the
cover?
method.The reason it prints 3,4,5, in your example is because it says if x is in the range from 3 to 5 print it.