From: Bertram Scharpf Date: 2005-05-07T06:41:11+09:00 Subject: Re: .. and ... aren't operators, maybe they should be? Hi, Am Freitag, 06. Mai 2005, 07:22:09 +0900 schrieb Logan Capaldo: > I was just wondering if maybe the range construction syntax could be > implemented as a set of operators? > > ... > This wouldn't implement the parser much (I don't think any way) and > it could be useful for things like > defining inifinite lazy lists, or other ranges with weird natures > > ... > > (1..inf).take(5) do |x| > p x Say `inf = 1/0.0', that works. > Ok thats a pretty silly example, but lets say succ was more > complicated than just self + 1 > > Eh maybe I'm crazy and this is useless. Just seems to be in line with > << and + etc. being changeable. You may redefine a classes `<=>' operator and its `succ' method before you use its instances as range limits. The compare operator is needed for initialization of the range and for the `include?' test. The `succ' method is used for `to_a' and `each'. Here is another infinity implementation: class InfClass def initialize ; @s = +1 ; end def negate ; @s = -@s ; self ; end def -@ ; dup.negate ; end def <=> oth ; @s ; end include Comparable end class String alias oldcmp <=> def <=> oth oldcmp( oth) || -(oth <=> self) end end Inf = InfClass.new puts Inf < "x" puts "x"..Inf puts -Inf.."x" This is not a very good example as `succ' doesn't make very much sense for an infinity value. Bertram -- Bertram Scharpf Stuttgart, Deutschland/Germany http://www.bertram-scharpf.de