Array#delete return value

Discussion in 'Ruby' started by Guillaume Marcais, Aug 5, 2004.

  1. According to the following contrived example (and the documentation),
    Array#delete returns the object passed as argument if a match is found
    and deleted:

    [gus@comp tmp]$ cat delete.rb
    Toto = Struct.new("Toto", :a, :b)
    class Toto; def ==(t); a == t.a; end; end
    t1 = Toto.new(1, 2)
    t2 = Toto.new(1, 3)
    p t1 == t2
    a = [t1]
    p t1.id
    p t2.id
    p a.delete(t2).id
    [gus@comp tmp]$ ruby delete.rb
    true
    578438406
    578438356
    578438356

    It seems to me it would make more sense to return the object actually
    removed from the array (t1 in the example instead of t2).

    The behavior I would expect is as follow:

    [gus@comp tmp]$ cat delete2.rb
    class Array
    def delete2(o)
    i = index(o)
    return nil if i.nil?
    delete_at(i)
    end
    end

    Toto = Struct.new("Toto", :a, :b)
    class Toto; def ==(t); a == t.a; end; end
    t1 = Toto.new(1, 2)
    t2 = Toto.new(1, 3)
    p t1 == t2
    a = [t1]
    p t1.id
    p t2.id
    p a.delete2(t2).id
    [gus@comp tmp]$ ruby delete2.rb
    true
    663778146
    663778096
    663778146

    Any thoughts on the matter.
    Guillaume.
    Guillaume Marcais, Aug 5, 2004
    #1
    1. Advertising

  2. Hi,

    In message "Array#delete return value"
    on 04/08/06, Guillaume Marcais <> writes:

    |According to the following contrived example (and the documentation),
    |Array#delete returns the object passed as argument if a match is found
    |and deleted:

    |It seems to me it would make more sense to return the object actually
    |removed from the array (t1 in the example instead of t2).

    But Array#delete method deletes ALL elements that equal to the
    argument. Do you want it to return an array of deleted items?

    matz.
    Yukihiro Matsumoto, Aug 6, 2004
    #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. PvdK
    Replies:
    0
    Views:
    2,978
  2. Seong-Kook Shin
    Replies:
    1
    Views:
    490
    Richard Bos
    Jun 18, 2004
  3. Greenhorn
    Replies:
    15
    Views:
    812
    Keith Thompson
    Mar 6, 2005
  4. X X
    Replies:
    4
    Views:
    350
    red floyd
    Jul 19, 2010
  5. Steve Anderson
    Replies:
    3
    Views:
    251
    Steve Anderson
    Jun 21, 2004
Loading...

Share This Page