From: Shin Nishiyama Date: 2003-09-05T13:27:56+09:00 Subject: Re: Difference between .. and ... in boolean ranges Hi! I think i can answer. When operator '..' or '...' is used in the condition expression, it works like a flip-flop switch and returns true/false (default is false). The difference of the two is the timing of evaluation. I think it is Perl-like. a=[1,2,'a',3,4,5,6,'b',7,8,9] a.each do |x| if (x == 'a')..(x.is_a? String) print x end end #=> a a.each do |x| if (x == 'a')...(x.is_a? String) print x end end #=> a3456b Oliver Dain wrote >I'm a bit confused by some Ruby behavior I'm seeing with ranges. As I >understand it the difference between > >0..5 > >and > >0...5 > >is that the former includes the number 5 while the 2nd does not. That >seems to be the case, but one would expect the same type of behavior >in boolean context. For example, I'd think > >0.upto(9) do |x| > if (x==3)..(x==5) > puts "In the range: #{x}" > else > puts "Not in the range: #{x}" > end >end > >Would output: > >Not in the range: 0 >Not in the range: 1 >Not in the range: 2 >In the range: 3 >In the range: 4 >In the range: 5 >Not in the range: 6 >Not in the range: 7 >Not in the range: 8 >Not in the range: 9 > >while using the 3 dot form like this: > >0.upto(9) do |x| > if (x==3)...(x==5) > puts "In the range: #{x}" > else > puts "Not in the range: #{x}" > end >end > >Would output: > >Not in the range: 0 >Not in the range: 1 >Not in the range: 2 >In the range: 3 >In the range: 4 >Not in the range: 5 >Not in the range: 6 >Not in the range: 7 >Not in the range: 8 >Not in the range: 9 > >However, the 3 dot form doesn't work as expected. It outputs the same >as the 2 dot form (which works as expected). What gives? This seems >unintuitive to me. Any thoughts? > >Thanks, >Oliver > > >