G
Guillaume Marcais
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.
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.