------ art_6403_15936890.1214344545423 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Tue, Jun 24, 2008 at 4:43 PM, Daniel Berger <djberg96 / gmail.com> wrote: > Hi all, > > In messing around with Array#delete I think I've uncovered a problem. > Consider this example: > > class Foo > def other) > $array.clear > false > end > end > > arr 1, 2, 3] > arr << Foo.new > Arr << 'b' > > $array rr > > arr.delete('a') > > p arr # [] > p $array # [] > > That's what I would expect. It did the comparison, and called the > Foo# method, which in turn cleared the receiver. > > But if you do this instead: > > arr.delete(1) > > You end up with: > > [nil, nil, #<Foo:0x2865ff4>] > [nil, nil, #<Foo:0x2865ff4>] > > What's happening? Well, I haven't dug into the implementation of Array#delete enough to fully explain the results but. You do know that after the assignment $array rr You've made the global variable $array reference the same object to which you are sending delete. So the $array.clear in the Foo# method is clearing that object which is the receiver of the delete method. Now in general ruby iterator methods don't expect the enumerated object to change during the iteration and that's just what's happening. This is no doubt confusing the delete method. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ ------ art_6403_15936890.1214344545423--