From: Florian Frank Date: 2005-11-26T07:47:30+09:00 Subject: Re: string range membership 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 == self } ? true : false end end "bb".element? "a".."zz" # => 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