On Wednesday 02 February 2011 03:30:15 Yossef Mendelssohn wrote:
> On Feb 1, 3:21 am, Stefano Crocco <stefano.cro... / alice.it> wrote:
> > class Range
> >   def each
> >     current = @start
> >     while (current <=> @end) < 1
> >       yield current
> >       current = current.succ
> >     end
> >   end
> > end
> 
> Your use of <=> is perplexing, given that anything Comparable would
> have a nicer operator for this purpose.
> 
> --
> -yossef

Yet, it's what Range uses. According to "The ruby programming language", for 
an object to be used as a Range endpoint, it needs to have a <=>. It doesn't 
need to include Comparable. Look at this:

class X
  def initialize n
    @n = n
  end
end

x1 = X.new(1)
x2 = X.new(5)

x1..x2
=> ArgumentError: bad value for range

class X
  def <=> other
    @n <=> other.instance_variable_get(:@n)
  end
end

x1..x2
=> #<X:0x8286ad8 @n=1>..#<X:0x8284ea4 @n=5>

As you see, you don't need to have a <, > or == method, just <=>.

Stefano