From: Niklas Frykholm Date: 2002-01-27T08:22:52+09:00 Subject: Re: Why isn't Range Comparable? [Dave]: > If a Range is an ordered set, which depends on it's members having > '<=>' and succ() methods, then why is there no Range#<=>, so to speak? > This simple experiment seems to work fine with a Range of Fixnums, but > I haven't tried other object types yet: > > irb(main):016:0* class Range > irb(main):017:1> def <=>(other) > irb(main):018:2> return 0 if self.member?(other) > irb(main):019:2> return -1 if other < self.begin > irb(main):020:2> return 1 if other > self.end > irb(main):021:2> raise "Bogus!" > irb(main):022:2> end > irb(main):023:1> end > > irb(main):025:0> (1..10) <=> 11 > 1 > irb(main):026:0> (1..10) <=> 0 > -1 > irb(main):027:0> (1..10) <=> 1 > 0 (You probably have -1 and 1 inverted above.) #<=> is usually used to compare objects of the same class, since ranges are not comparable, calling this method #<=> is probably confusing. I'm not even sure fixnums and ranges of fixnums can always be compared in a sane manner. What would you have this return. (10..1) <=> 3 // Niklas