On Thu, Oct 19, 2006 at 07:37:02PM +0900, Florent Guiliani wrote:
> First impressions of ranges:
> ---------------------------
> Now let's try ranges as conditions:
>
> irb(main):282:0* a=[ "aaa", "bbbb", "bbbbbb", "ccc", "dddddd" ]
> => ["aaa", "bbbb", "bbbbbb", "ccc", "dddddd"]
> irb(main):283:0> a.each { |b| puts b if b=~/b/..b=~/d/ }
> bbbb
> bbbbbb
> ccc
> dddddd
> => ["aaa", "bbbb", "bbbbbb", "ccc", "dddddd"]
> irb(main):284:0> a.each { |b| puts b if b=~/b/...b=~/d/ }
> bbbb
> bbbbbb
> ccc
> dddddd
> => ["aaa", "bbbb", "bbbbbb", "ccc", "dddddd"]
>
> "Oups it did not it again!"
>
> .. and ... seems not working on the same way if you use ranges as
> sequences or ranges as conditions. My point of view: (unless I'm making
> a mistake) It gets confusing.

The difference between .. and ... in a conditional is a little different
than when used in a range.  This is described in the PickAxe book on p
327 Second Edition PDF 2007-09-20.  The difference is that the two-dot
form evaluates the second condition when it enters the true state and
the three-dot form does not.  So, for example, on a given iteration if
the first conditional caused the ``state machine'' to go into the set
state and the second conditional evaluated to true, the two-dot form
would return true but switch the state back to unset and the three-dot
form would not.  Here is the example code from the PickAxe book (spruced
up a bit):

#! /usr/bin/ruby -w
# test behavior of range operator in conditional
require 'pp'
a = (11..20).collect {|i| (i % 4 == 0)..(i % 3 == 0) ? i : nil}
pp(a)
b = (11..20).collect {|i| (i % 4 == 0)...(i % 3 == 0) ? i : nil}
pp(b)

Which outputs:

[nil, 12, nil, nil, nil, 16, 17, 18, nil, 20]
[nil, 12, 13, 14, 15, 16, 17, 18, nil, 20]

The two-dot form evaluates both conditionals when i is 12, it returns
true but since the second condition evaulates to true the state machine
is changed back to unset and subsequent values do not get printed until
the first contitional returns true again.

Note that even when the second conditional returns true and the state
machine is switched to unset, the expression returns true.  This is true
for both forms.

--
David Dooling