===, include?, =~

Discussion in 'Ruby' started by gwtmp01@mac.com, Oct 27, 2005.

  1. Guest

    I started this post after wondering why
    Set doesn't alias :=== to :include?

    That led me to look at ===, include?, and =~ for a variety
    of classes...

    The following table shows the results of executing
    pattern.send(method, arg)

    Pattern Arg === include? =~
    /foo/ "foo" yes fails yes
    0..10 5 yes yes Object#=~
    ["foo"] "foo" Array#== yes Object#=~
    Set.new ["foo"] "foo" Set#== yes Object#=~
    {"foo", nil} "foo" Hash#== yes Object#=~
    String "foo" yes fails Object#=~

    yes means that a class specific method definition exists.
    Object#=~ always returns false.
    Regexp#=== is the same as Regexp#=~
    Range#=== is the same as Range#include?

    Could ===, include?, and =~ all be unified?
    Does anyone have an example where the semantics of these
    three methods should be different (other than for
    backward compatibility)?

    For the "collection" classes (Array, Set, Hash), the ===
    method defaults to using the class specific == method.
    Does it make sense for === to be semantically equivalent to ==
    for "collection" classes? It seems quite reasonable for "value"
    classes where case/when/end can be used to look for particular
    values but for a collection class a "pattern" match behavior
    would seem more useful when the argument is not also a collection.

    Collection === element # membership/inclusion test
    Collection === Collection # == test

    Are there any examples where =~ and === are defined but aren't
    aliases for each other?

    Minimally, it would seem that Regexp#include? could be aliased
    to Regexp#=~ and Class#include? could be aliased to Class#===
    This would make collection.include?(element) a "pattern" test
    for all the classes I looked at.
     
    , Oct 27, 2005
    #1
    1. Advertising

  2. Eric Mahurin Guest

    --- wrote:

    > I started this post after wondering why
    > Set doesn't alias :=3D=3D=3D to :include?
    >=20
    > That led me to look at =3D=3D=3D, include?, and =3D~ for a variety
    > of classes...


    This was the main reason that sent me off making Object#duck to
    map method names to the way you need:

    http://rcrchive.net/rcr/show/318

    My most common usage now is:

    set.duck:)=3D=3D=3D,:include?)

    It still puzzles me why some people wouldn't want something
    like this in the ruby core.

    I use this quite a bit with my grammar project. Several of my
    basic methods need an argument that responds to =3D=3D=3D.



    =09
    =09
    __________________________________=20
    Yahoo! Mail - PC Magazine Editors' Choice 2005=20
    http://mail.yahoo.com
     
    Eric Mahurin, Oct 27, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danny Anderson
    Replies:
    5
    Views:
    517
    Victor Bazarov
    Aug 15, 2003
  2. Rolf Magnus
    Replies:
    2
    Views:
    617
    Karl Heinz Buchegger
    Nov 28, 2003
  3. Elie Nader
    Replies:
    1
    Views:
    651
  4. Aguilar, James
    Replies:
    2
    Views:
    707
    Aguilar, James
    Jul 16, 2004
  5. Andreas Bogenberger
    Replies:
    3
    Views:
    959
    Andreas Bogenberger
    Feb 22, 2008
Loading...

Share This Page