[#3006] CVS repository — "Eugene Scripnik" <hoaz@...>

Hello.

21 messages 2004/06/16
[#3008] Re: CVS repository — ts <decoux@...> 2004/06/16

>>>>> "E" == Eugene Scripnik <hoaz@gala.net> writes:

[#3009] Re: CVS repository — Michal Rokos <michal@...> 2004/06/16

Hi!

[#3057] Ruby 1.8.2 to be released. — matz@... (Yukihiro Matsumoto)

Hi,

20 messages 2004/06/23

Re: member? / include?

From: Johan Holmberg <holmberg@...>
Date: 2004-06-05 13:21:41 UTC
List: ruby-core #2967
On Sat, 5 Jun 2004, Johan Holmberg wrote:
>
> Another example (beside entries/to_a) of halting synonyms is
> Enumerable#member? and Enumerable#include?. [...]

Yet another example of difference, this time in Range:

    p ("aa".."az").include?( "alpha" )       # => true
    p ("aa".."az").member?(  "alpha" )       # => false

    p ("aa".."az").entries.include?( "alpha" )    # => false
    p ("aa".."az").entries.member?(  "alpha" )    # => false

    p (1..10).include?( 3.14 )               # => true
    p (1..10).member?(  3.14 )               # => false

I guess this is because Range tries to be both a discrete range and
a "continuous" range. The documentation is not clear about this
difference:

     $ ri Range.include\?
     [...]
     Returns +true+ if _obj_ is an element of _rng_, +false+
     otherwise. Conveniently, +===+ is the comparison operator used
     by +case+ statements.
     [...]
     $ ri Range.member\?
     [...]
     Return +true+ if _val_ is one of the values in _rng_ (that is
     if +Range#each+ would return _val_ at some point).
     [...]

If one looks at both these documentation entries it is easy to see
that there *might be* a difference between the methods (since the
text is different). But I don't think it is obvious what difference
there is between "is an element of _rng_" and "is one of the values
in _rng_", especially if one reads one of these entries in
isolation.

It should probably be pointed out in the documentation that
Range#include? tests according to <=> to see if _obj_ is inside the
interval given by the endpoints of the Range.

As I said in an earlier mail, my spontaneous opinion is that it
would be more natural if the synonym-relation between
include?/member? was preserved in the classes including Enumerable.
Then what is now Range#include?, could be renamed to something more
descriptive (e.g. Range#inside? or something similar).

Or maybe all these aliases should be removed altogether ?
But I guess that is out of the question ;-)

/Johan Holmberg


In This Thread

Prev Next