Hi --

On Thu, 25 Oct 2007, Todd Benson wrote:

> On 10/24/07, Todd Benson <caduceass / gmail.com> wrote:
>> On 10/24/07, Bob Hutchison <hutch / recursive.ca> 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.

I'm not seeing what you're seeing:

irb(main):014:0> a = 1,2,3
=> [1, 2, 3]
irb(main):015:0> a[0].__id__
=> 3
irb(main):016:0> b = a
=> [1, 2, 3]
irb(main):017:0> b.__id__
=> 1669980                # Are you really seeing 3 here?
irb(main):018:0> a[0] = 7
=> 7
irb(main):019:0> a[0].__id__
=> 15
irb(main):020:0> b.__id__
=> 1669980                # And here?

When you do b = a, you're binding b to the same object as a. I don't
know why b would think you had bound it to a[0].


David

-- 
Upcoming training by David A. Black/Ruby Power and Light, LLC:
   * Advancing With Rails, Edison, NJ, November 6-9
   * Advancing With Rails, Berlin, Germany, November 19-22
   * Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details!