From: David Vallner Date: 2006-01-14T06:58:04+09:00 Subject: Re: Range#member? Oddity On Fri, 13 Jan 2006 22:16:14 +0100, Matthew Desmarais wrote: > James Edward Gray II wrote: >> I'm not understanding what I am seeing here. Can anyone please explain >> why the last line of this session gives *false* as an answer? >> >> >> range = ("1".."10") >> => "1".."10" >> >> range.to_a >> => ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] >> >> range.member?("1") >> => true >> >> range.member?("2") >> => false >> >> James Edward Gray II >> >> >> > Hi, > > There was some discussion about this in the recent past. If my memory > serves me right (certainly an infrequent happening), the issue that > you're running into is that Range#member? is implemented as: > class Range > def member?(val) > if self.exclude_end? > (self.first <= val) and (val < self.last) > else > (self.first <= val) and (val <= self.last) > end > end > end > > You should find this in both 1.8.2 and 1.8.4 I think. > > There's a previous thread on ruby-talk about it, here's a link to > somewhere near the conclusion of the discussion: > http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/167194 > > Matthew > This would seem to make it an oddity of String#succ, that behaving automagically, not generating successors with respect to String comparison. E.g. for any Integers i, i.succ > i. For some strings, that does't hold true. Bottom line: Don't use strings when you're really using numbers. Like in mathemathical contexts. D'oh. You could possibly hack around that in Range code to provide for data types where generating successors is inconsistent with comparison, but I wouldn't like to see that. David Vallner