On Saturday, July 19, 2003, at 01:58 AM, 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...
>
> -Kurt

While I was writing this message, Phil responded with some similar 
thoughts, but I will post anyway, with apologies for the areas of 
duplication.

A few of thoughts:

1. It seems that in python '1 < 2 < 3' is sugar for '1 is less than 2 
and 2 is less than 3'.

2. How does python handle '1 < 2 < 3 < 4'? How does python handle '1 < 
2 or 5 < 4'?

2. In Ruby 1 < 2 is a representation of the Fixnum object 1 calling the 
'<' method with argument Fixnum object 2. The object returned is 'true' 
(an object of the TrueClass). In the expression '1 < 2 < 3', the 'true' 
object calls the '<' method with argument '3', an a no method error is 
raised.

3. You can write the expression in Ruby as '1 < 2 and 2 < 3'.

4. You could also modify the '<'  method to return the value of the 
argument given if it evaluates to 'true' -- however, you would have to 
parse the output so that the last comparison returns 'true' and not the 
last argument. The following just modifies the return value of '<' and 
does not handle the problem of the method chain now returning the last 
argument:

class Fixnum
   alias old_less_than <
   def < (arg)
     if self.old_less_than(arg)
       return arg
     end
   end
end

1 < 2 < 3 => 3

5. You could also parse input so that '1 < 2 < 3' is translated to '1 < 
2 and 2 < 3'. I haven't thought about how to do this from within Ruby.

6. I prefer the explicit '1 < 2 and 2 < 3'.

Regards,

Mark