From: Abder-Rahman Ali on
In the "Why's poignant guide to Ruby" book, it states the following:

"If you can't get to an object through a variable, then Ruby will figure
you are done with it and will get rid of it. Periodically, Ruby sends
out its garbage collector to set these objects free."

The point I'm not getting here is: "If you can't get to an object
through a variable..."

Can you provide an examples that illustrates this?

Thanks.
--
Posted via http://www.ruby-forum.com/.

From: Bruno A on
On 13-07-2010 17:30, Abder-Rahman Ali wrote:
> In the "Why's poignant guide to Ruby" book, it states the following:
>
> "If you can't get to an object through a variable, then Ruby will figure
> you are done with it and will get rid of it. Periodically, Ruby sends
> out its garbage collector to set these objects free."
>
> The point I'm not getting here is: "If you can't get to an object
> through a variable..."
>
> Can you provide an examples that illustrates this?
>
> Thanks.

a = [1,2,3].find {|x| x > 3}

puts a


In this (maybe too simple) example, x is garbage-collected once flow
leaves the block.


--
http://iruel.net
From: James Harrison on
The simplest way to think of it is if something falls out of scope.


On Jul 13, 2010, at 9:30 AM 7/13/10, Abder-Rahman Ali wrote:

> In the "Why's poignant guide to Ruby" book, it states the following:
>
> "If you can't get to an object through a variable, then Ruby will figure
> you are done with it and will get rid of it. Periodically, Ruby sends
> out its garbage collector to set these objects free."
>
> The point I'm not getting here is: "If you can't get to an object
> through a variable..."
>
> Can you provide an examples that illustrates this?
>
> Thanks.
> --
> Posted via http://www.ruby-forum.com/.
>


From: Abder-Rahman Ali on
James Harrison wrote:
> The simplest way to think of it is if something falls out of scope.

Thanks James. Can you provide a simple Ruby code to clarify the idea. A
bit confusing me still.
--
Posted via http://www.ruby-forum.com/.

From: James Harrison on

On Jul 13, 2010, at 9:42 AM 7/13/10, Abder-Rahman Ali wrote:

> James Harrison wrote:
>> The simplest way to think of it is if something falls out of scope.
>
> Thanks James. Can you provide a simple Ruby code to clarify the idea. A
> bit confusing me still.

Sure thing. Sounds like you might be new to programming, yeah? Scope is something you may need to look up to understand a little better. In ruby there are a few scope rules that you should look up and get familiar with, and they changed for 1.9.1 in some small ways (I believe...). The MetaProgramming Ruby book has some great insights into scope, too.

Simple, here's an example:

words = ["foo", "bam", "bat"]

words.each do |word| #here we create a variable called "word"
puts "#{word}" #here we print out the contents of the "word" variable
end #here we end the block that word was created inside of

puts "#{word}" #this will fail, as word does not exist outside of the block

word is a variable that is local to the block associated with the .each call on the array. Once the block ends, the variable word stops existing: it "falls out of scope".

Go read up on scoping and scope generally, it'll do you good ;)

Best

James