Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Array#delete return value

Reply
Thread Tools

Array#delete return value

 
 
Guillaume Marcais
Guest
Posts: n/a
 
      08-05-2004
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.



 
Reply With Quote
 
 
 
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      08-06-2004
Hi,

In message "Array#delete return value"
on 04/08/06, Guillaume Marcais <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function take in value and return value CSINVA ASP .Net 6 11-07-2007 12:49 PM
Getting ID, calling url, search for value, return value Tim Fröglich ASP .Net Web Services 1 01-10-2006 09:18 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM
Return a return value from Perl to Javascript PvdK Perl 0 07-24-2003 09:20 AM



Advertisments