From: Logan Capaldo Date: 2005-11-27T06:49:50+09:00 Subject: Re: string range membership ------=_Part_30806_8686771.1133041785218 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 11/25/05, Florian Frank wrote: > > daz wrote: > > >Jim Weirich wrote > > > > > >>How about "within?" for a value within a given range? > >> > >> > >> > > > > (0..5).within?(3) > > > >reads backwards, IMO compared to: > > > > (0..5).include?(3) > > > > > Yes, I agree. I think, just finding a new word for a method that does > something, that used to have different names in the past, won't help. > This has been tried, but it didn't work too well. > > Ruby's ranges have (at least) a dual nature: > 1. as an interval (a, b) of values, > 2. as a shortcut for a set of values { a, a.succ, a.succ.succ, ..., b }. > > I think "include?" is a good name for the 1. And 2 is very similar to 1, > so people will easily confuse those names. > > What about using a bit of double dispatch here, like that: > > class Object > def element?(r) > r.find { |x| x =3D=3D self } ? true : false > end > end > > "bb".element? "a".."zz" # =3D> true > > This doesn't read backwards, and the name conveys the meaning of set > membership, as required by 2. > > Perhaps using another method than "find" for searching (that only > defaults to "find") would make it possible, to provide an alternative > implementation for datastructures, that can compute membership faster > than O(n). > > -- > Florian Frank > > > I was going to suggest r.has_element?(x) for the equavilent of #member. maybe r.surrounds?(x) for for #include. That one is not as good. ------=_Part_30806_8686771.1133041785218--