On Sat, Jul 19, 2003 at 02:58:51PM +0900, Kurt M. Dresner wrote:
> When I learned python I was overjoyed that I could evaluate 1 < 2 < 3
> and get "true".  I just realized that you can't do that in Ruby.  Is
> there a reason why?  Is it good?  I know I can use "between", but
> still...

You can also use range objects for a clean alternative to a <= b <= c :

   x = 2   # or 1 or 3
   if (1..3) === x
      puts "It's in the range"
   end

The strange 'backwards' ordering is to support case statements, which make
it look much clearer:

   case x
   when (1..3)
     puts "It's in the lower range"
   when (4..6)
     puts "It's in the upper range"
   else
     puts "It's out of range"
   end

In the above, 3.5 is also 'out of range'. But the three-dot form of range
supports a <= b < c

   puts "nope" if (1...3) === 3.0    # false
   puts "yep " if (1...3) === 2.99   # true

   case x
   when (1...4)  # 1 to 3.999
   when (4...7)  # 4 to 6.999
   when (7...10) # 7 to 9.999
   end

Cheers,

Brian.