Array#===

Discussion in 'Ruby' started by Martin DeMello, Feb 19, 2005.

  1. It seems a bit of a waste for Array#=== to default to Object#=== - it'd
    be far more useful, IMO, to have it call include?. Can anyone see a
    drawback to this?

    Here's a real-world use case from the FXIrb code:

    def onKeyPress(sender,sel,event)
    case event.code
    when Fox::KEY_Delete,Fox::KEY_KP_Delete,Fox::KEY_BackSpace
    if getCursorPos > @anchor
    super
    end

    which a change to Array#=== would let me write as

    config[:delete_keys] =
    [Fox::KEY_Delete,Fox::KEY_KP_Delete,Fox::KEY_BackSpace]
     
    Martin DeMello, Feb 19, 2005
    #1
    1. Advertising

  2. Martin DeMello

    Mark Hubbart Guest

    On Sun, 20 Feb 2005 07:04:44 +0900, Martin DeMello
    <> wrote:
    > It seems a bit of a waste for Array#=== to default to Object#=== - it'd
    > be far more useful, IMO, to have it call include?. Can anyone see a
    > drawback to this?


    I've wondered about this too. It seems that the === method loosely
    translates to a membership test, so it would make sense if it tested
    membership with arrays, as well as ranges and classes and regexps. I
    can't see any drawbacks, though. (though that doesn't mean there
    aren't any)

    cheers,
    Mark
     
    Mark Hubbart, Feb 19, 2005
    #2
    1. Advertising

  3. Martin DeMello wrote:
    > It seems a bit of a waste for Array#=== to default to Object#=== - it'd
    > be far more useful, IMO, to have it call include?. Can anyone see a
    > drawback to this?
    >
    > Here's a real-world use case from the FXIrb code:
    >
    > def onKeyPress(sender,sel,event)
    > case event.code
    > when Fox::KEY_Delete,Fox::KEY_KP_Delete,Fox::KEY_BackSpace
    > if getCursorPos > @anchor
    > super
    > end
    >
    > which a change to Array#=== would let me write as
    >
    > config[:delete_keys] =
    > [Fox::KEY_Delete,Fox::KEY_KP_Delete,Fox::KEY_BackSpace]
    > .
    > .
    > .
    > when config[:delete_keys]
    > __etc__
    >
    >
    > martin


    How about using the splat idiom?

    a = [1,2,3]
    case 3
    when *a; p a
    end
     
    Joel VanderWerf, Feb 20, 2005
    #3
  4. Martin DeMello wrote:
    > It seems a bit of a waste for Array#=== to default to Object#=== - it'd
    > be far more useful, IMO, to have it call include?. Can anyone see a
    > drawback to this?


    We'd lose this, FWIW:

    points = [
    [1,2],
    [3,4],
    [0,0]
    ]

    points.each do |point|
    case point
    when [0,0]
    puts "origin!"
    end
    end

    To be a bit philosophical, I'd say that Arrays, unlike Regexps, do not
    exist primarily to match, and so #=== should be Object#===.
     
    Joel VanderWerf, Feb 20, 2005
    #4
  5. Joel VanderWerf <> wrote:

    > How about using the splat idiom?
    >
    > a = [1,2,3]
    > case 3
    > when *a; p a
    > end


    Very neat - didn't think of that.

    martin
     
    Martin DeMello, Feb 20, 2005
    #5
    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. Daniel
    Replies:
    1
    Views:
    1,071
    Kevin Collins
    Aug 14, 2003
  2. S300
    Replies:
    4
    Views:
    22,121
    Roedy Green
    Aug 19, 2003
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    521
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    234
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    522
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page