From: al_batuul Date: 2007-11-19T21:44:36+09:00 Subject: Re: Open-ended ranges? --0-820436838-1195475878=:77926 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Dear Clifford, Defining an open-end class is a great idea; the rang from 1..nil or 0 to nil could help now, I suggest you add different types of parameters to the class such as time. all the best, al_batuul Clifford Heath wrote: Folk, I found Sean Russell's 2001 posting on this subject, and had a play with creating open ranges using of class instances - interesting to find you can even do this! What I really want is to be able to define ranges like (1..nil), which is an open-ended range starting from 1. Of course this doesn't work, but not for the reason I expected. There seems to be some magic inside MRI that prevents it. Like if I define: class End attr_reader :e def initialize(e) @e = e end def <=>(other) @e <=> other.e end def succ @e && @e.succ end end Then I can create the following range: End.new(1) .. End.new(4) but not this: End.new(1) .. End.new(nil) Ok, perhaps that's fair enough. But inside class End, replace all references to @e by @e.to_i (just for the experiment), and it works (at least, it bitches that 0 is less than 1, but use End.new(-1) .. End.new(nil) and it's ok). Now add puts "#{@e.to_i} <=> #{other.e}"; into the "<=>" method, and Range "knows" that's not ok... when to my mind it should be ok if the previous version was. I haven't taken this any further yet. Until I know that some internal magic isn't going to stop me making a Range class that works as advertised, it didn't seem worth pursuing. What do you think? Clifford Heath. --------------------------------- Never miss a thing. Make Yahoo your homepage. --0-820436838-1195475878=:77926--