From: Logan Capaldo Date: 2005-05-07T05:51:41+09:00 Subject: Re: .. and ... aren't operators, maybe they should be? On 5/5/05, Logan Capaldo wrote: > I was just wondering if maybe the range construction syntax could be > implemented as a set of operators? > > ie > > class Object > def ..(other) > Range.new(self, other) > end > def ...(other) > Range.new(self, other, true) > end > end > > 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 > > ie > > class InfRange > attr_reader :first > def take(n) > i = self.first > 1.upto(n) { yield(i); i = i.succ } > end > def initialize(n) > @first = n > end > end > > class Fixnum > def ..(other) > if other.nil? > InfRange.new(self) > else > Range.new(self, other) > end > end > def ...(other) > if other.nil? > InfRange.new(self) > else > Range.new(self, other, true) > end > end > end > > (1..inf).take(5) do |x| > p x > end > > 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. > Nobody likes this idea? Or is it just so dumb its not worthy of a response? ;)