From: Konrad Meyer Date: 2007-11-19T11:10:51+09:00 Subject: Re: Open-ended ranges? --nextPart15135936.UXfa22MGNh Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Quoth Clifford Heath: > Folk, >=20 > I found Sean Russell's 2001 posting on this subject, and had a play with= =20 creating > open ranges using of class instances - interesting to find you can even d= o=20 this! >=20 > 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 fo= r=20 the > reason I expected. There seems to be some magic inside MRI that prevents = it. >=20 > Like if I define: >=20 > class End > attr_reader :e > def initialize(e) > @e =3D e > end > def <=3D>(other) > @e <=3D> other.e > end > def succ > @e && @e.succ > end > end >=20 > Then I can create the following range: End.new(1) .. End.new(4) > but not this: End.new(1) .. End.new(nil) >=20 > 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). >=20 > Now add puts "#{@e.to_i} <=3D> #{other.e}"; into the "<=3D>" method, and > Range "knows" that's not ok... when to my mind it should be ok if the > previous version was. >=20 > 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 advertise= d, > it didn't seem worth pursuing. >=20 > What do you think? >=20 > Clifford Heath. Then again, there is this already: irb(main):001:0> 0..(1.0/0) =3D> 0..Infinity HTH, =2D-=20 Konrad Meyer http://konrad.sobertillnoon.com/ --nextPart15135936.UXfa22MGNh Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQBHQPCtCHB0oCiR2cwRAkC5AKDQtyXoclx7b9k2ChuTdcAVTTuZ6gCfb3ES Am1AYvaMlnnD9rcD0WmugtY= =nF0E -----END PGP SIGNATURE----- --nextPart15135936.UXfa22MGNh--