From: David Flanagan Date: 2008-03-27T05:48:34+09:00 Subject: Re: Recent changes in Range#step behavior Dave Thomas wrote: > > On Mar 26, 2008, at 1:15 PM, Vladimir Sizikov wrote: >> >> Oh, btw, not only floats but also Rationals (and maybe some other >> custom Numeric types). >> >> And such changes would invalidate the "The Ruby programming language" >> book, section "Ranges": >> >> " If the endpoints of the range are numbers, these methods use the >> continuous membership test, just as they did in Ruby 1.8. If the >> endpoints are not numeric, however, they instead use the discrete >> membership test. " > > I'm updating that constantly as things change--that's the joy (and pain) > of the beta process... :) > Hey Dave! Vladimir was quoting my book, not yours! :-) Thanks for noting the book compatibility issue, Vladimir. I have several comments on this topic: 0) I assume that Matz had well-thought-out reasons for implementing the current behavior and that these are based on real-world utility. 1) I don't think it is worth breaking compatibility with 1.8 over this issue. And I think changing include? and member? would break a substantial amount of code. 2) I don't believe it is correct to regard Ranges as sets of values. They are something different and can have continuous or discrete behavior depending on how they are used. If you define a range as a kind of set, then the behavior of include? and member? seems wrong. But if you instead look at how those methods behave in 1.8 and 1.9 then it becomes clear that a range is not a set. 3) For ranges with integer endpoints, using succ is not a reasonable way to implement member? and include?--it is too slow compared to <=>. Therefore a discrete membership test is likely to involve some kind of type checking--to ensure that the argument is of the same class as the endpoints. And I suspect that this might make some people uncomfortable. 4) I'd suggest that include? and member? be left as they are with their complex (sometimes discrete sometimes continuous) but backward-compatible behavior. Ruby 1.9 already introduces a new method cover? that guarantees a continuous membership test. Let's add yet another new method that guarantees a discrete membership test. This new method could be called has?. If cover? and has? prove satisfactory for Ruby 1.9 developers, then include? and member? could possibly be deprecated in 2.0. David