From: Brian Candler Date: 2004-10-05T18:59:04+09:00 Subject: Re: [RCR] New [] Semantics On Tue, Oct 05, 2004 at 06:19:09PM +0900, trans. (T. Onoma) wrote: > I occurs to me that the angry villagers might be confused. The example of the > never ending > > (0..(10.0/0)).member?(4) > > comes to mind. Why would this be an infinite loop? It must be trying to > generate the list before looking to see if 4 is in it (?) Yes, see adjacent thread. What it actually does is iterate all value from start to end using succ, and set a flag to true when it finds a match (but it doesn't break out of the loop when a match is found) > Are these ranges > that stupid? Yes, but what you probably want is 'include?' rather than 'member?' include? just checks the given value against the start and end values. Both Range#include? and Range#member? override the methods mixed in from Enumerable, where those methods are just synonyms for each other. This is certainly confusing! I'd say if you want to iterate over the range, then use Enumerable#find or Enumerable#find_all as appropriate, then get rid of this distinction. > Yuk. But there is nothing one can do about it as long as one depends on #succ. > I suppose it's awfully clever and OOP and all to have any object supporting > <=> and succ work with ranges, but I wonder how much use they get outside of > numbers and occasional character ranges. In other words perhaps succ isn;t > the way to go (or perhaps a fallback) and a simple increment/decrement in the > Range itself would be more usable --then the above 7 minutes would be about 7 > milliseconds. If you're going to rely on increment/decrement then I'm pretty sure you can also rely on the mathematic properties of < and >, i.e. just compare the boundary values as 'include?' does. Besides, a = a.succ and a = a + 1 take almost identical amounts of time, since even '+ 1' involves a method dispatch: a = a.send(:+,1) Regards, Brian.