From: Todd Benson Date: 2007-10-25T00:21:47+09:00 Subject: Re: pop/push, shift/unshift On 10/24/07, Todd Benson wrote: > On 10/24/07, Bob Hutchison wrote: > > > > On 24-Oct-07, at 9:03 AM, Todd Benson wrote: > > > > > def dequeue > > > @list.shift > > > end > > > > Save yourself some grief, do it this way: > > > > def dequeue > > tmp = @list[0] > > @list[0] = nil > > @list.shift > > tmp > > end > > > > Cheers, > > Bob > > Bob, I can only guess that the grief you are talking about has > something to do with memory management, but I don't see it. > > Can someone explain -- with the code above -- why tmp does in fact > _not_ take on NilClass? I think it would be good info for newbies. > > Todd Okay, I guess I can answer my own question. Binding is resolved when an operator like '=' is interpreted, especially with immutable objects (like Fixnum). a = 1,2,3 puts a[0].__id__ #output => 3 b = a puts b.__id__ #output => 3 a[0] = 7 puts a[0].__id__ #output => 15 puts b.__id__ #output => 3 For the newbies out there, keep this in mind. The name b put on a[0]'s shoes and kept them on. Somebody correct me if I'm wrong. Todd