From: Robert Klemme Date: 2007-11-19T17:53:12+09:00 Subject: Re: Open-ended ranges? 2007/11/19, Clifford Heath : > Todd Benson wrote: > > Firstly Todd, thanks for quoting Konrad, as his messages aren't getting to the newsgroup. > > > Somebody please explain to me what good is a Range object that goes > > from 1 to something_undefined? > > I have a meta-language in which it's possible to define value restrictions, > where a value restriction is a list of values or value ranges, including > open-ended ones. I never plan to iterate over an open-ended range (though > I'd expect such a loop to be terminated by exception or some-such), merely > to detect whether a value is allowed by the restriction or not, i.e., either > matches one of the single values or falls inside one of the ranges. > > Apart from the open endedness of the ranges in this language (which is not > of my design), the Range object serves perfectly. The Infinity and -Infinity > will serve for numbers, but not for open-ended string ranges. > > > Set.new(1..nil) - Set.new(5..nil) > > > > Is nil supposed to represent infinity? > > No, Infinity will serve for that... but not for Strings. Perhaps the empty > string will serve? It seems to be able to go on either end of a range. > ("".."a").each only calls the block once, passing the Range. If you are willing to sacrifice the two or three dots then there is also another solution which nicely integrates with the language: OpenEnded = Struct.new :start do include Enumerable def each x = self.start loop do yield x x = x.succ end self end end for i in OpenEnded.new 10 puts i break if i > 20 end Kind regards robert -- use.inject do |as, often| as.you_can - without end