From: Randy Kramer Date: 2006-05-15T22:21:28+09:00 Subject: Re: begining programmer questions I'm not the original OP, but found this post quite helpful. But, in the interest of knowing what's going on under the hood, I wanted to expound on some things (based on my understanding) to find out if my understanding is correct: On Sunday 14 May 2006 08:50 pm, Daniel Baird wrote: > You're just missing the idea that variable names kinda > "point" at objects. > > The object pointed at might be an array, or a string, or whatever.. with a > whiteboard it's quite easy to illustrate the idea, but let's try this: > > when you say: > info = "hello there" > > ..that makes a pointer from the variable "info" to the string "hello > there". Here's a picture: > > info ----------> String "hello there" Actually, to be a little more pedantic, it creates a string in memory containing "hello there" and then creates a variable info which points to it. (?) > When you say: > > info = [] > > ..that makes a pointer from the variable "info" to a new, empty array. Again, this creates a new, empty array in memory, then changes the contents of the variable info to point to it instead of to the string containing "hello there" (that may be a stilted way of saying it). The string "hello there" still exists in memory, but (unless something else has been done, like another variable created (not mentioned so far) pointing to that string), it is no longer accessible and is subject to garbage collection (when Ruby, in it's infinite wisdom, decides it is appropriate to collect garbage). (?) Randy Kramer PS: Just to go off on a tangent, for a (poor) programmer with experience in some other languages, in some other languages, the: info = "hello there" statement might create a string in memory and permanently associate "info" with the address of that string. If someone then tried: info = [] either that address in memory would have "hello string" wiped out and replaced with an empty array, or (in a typed language), the statement would fail because the types don't match. > Doesn't matter what info used to "point" to.. it's now pointing at an > array. Like this: > > info ----------> Array [] > > > Hope that helps a bit! > > ;D