views:

1030

answers:

7

I'm not sure I understand scope - does an out-of-scope variable (I'm using Ruby) exist in memory somewhere or does it stop existing (I know you can't access it). Would it be inaccurate to say that an out-of-scope variable does not exist any more?

Maybe this is a philosophical question.

+1  A: 

It exists for a little bit until the garbage collector disposes it (if it can).

cherouvim
+2  A: 

When it goes out of scope it still exists (in the sense that it has some memory allocated to it) for some time, until garbage collection cleans it up. But as you imply, it's lost it's name and is unreachable.

Charlie Martin
+1  A: 

When a variable falls out of scope is anyone around to hear it scream?

This isn't a ruby question so much as a general question about garbage collection. In a garbage collected language such as Ruby or C# when a variable falls out of scope it's marked in some manner that says it's no longer in use. When this happens you can't get at it any more and it sits around twiddling its thumbs - but it does still have memory allocated to it.

At some point the garbage collector will wake up and look for variables marked as not in use. It will dispose of them and at that point they're no longer in memory at all.

It can be more complicated than this, depending on how the garbage collector works, but it's close enough :)

blowdart
+2  A: 

A variable is not the same as the value it holds.

The variable itself ceases to exist when it goes out of scope. The value that the variable held may represent an object, and that object may continue to exist beyond the lifetime of the variable. The garbage collector reclaims the object later.

Rob Kennedy
+1  A: 

Rob Kennedy has this answered appropriately, but I thought I would add a little more detail.

The important thing to recognize is the difference between a variable and the value it represents.

Here's an example (in C# because I don't know Ruby):

object c = null;
if (1 == 1) // Just to get a different scope
{
    var newObj = new SomeClass();
    newObj.SomeProperty = true;
    c = newObj;
}

In the code above, newObj goes out of scope at the end of the if statement and as such "doesn't exist", but the value that it was referring to is still alive and well, referenced by c. Once all of the references to the object are gone, then the garbage collector will take care of cleaning it up.

Chris Shaffer
+4  A: 

If you are using managed language then you don't allocate and unallocate memory so as far as you are concerned it no longer exists.

Technically it does but GCs tend not to be deterministic so technically its hard to say when it actually vanishes.

Quibblesome
It exists the same way Schrodinger's cat does.
Sarah Mei
lol ... now I am going to call all my temp variables cat
Toby Hede
A: 

If you're talking about file objects, it becomes more than a philosophical question. If I recall correctly, files do not close automatically when they go out of scope - they only close if you ask them to close, or if you use a "File.open do |f|" style block, or if they get garbage collected. This can be an issue if other code (or unit tests) try to read the contents of that file and it hasn't yet been flushed.

Andrew Grimm