[Ruby] Ghetto

javier ramirez jramirez at aspgems.com
Thu Jan 3 11:36:40 PST 2008


Hi,

>> user = User.new  #here i assign the new object to a variable. Reference count for the object_id will be increased
>> user = nil       #here we are decreasing the reference count for that object
>> GC.start        # reference count for that object_id should be 0 by now, so GC will 
> I'm assuming you executed your code in irb, 
you are assuming right, but you didn't copied my code but the original one.
> irb(main):002:0> User.new
> => #<User:0xb7c80d7c>
>   

that one should be user = User.new  according to the code I posted, then 
the assignment to nil and then the call to the GC
> p returns nil, the object assigned to the user variable is not nil.
>   

if you do assign the variable and execute again on irb you'll see no 
trace of the user object anymore
> Anyway, Eric explained what is going on with the reference in the C stack
>   

Yes, he explained it and it's true for the original code. I was just 
trying to clarify how with a little change in the code, by using a 
variable as the container of the object, the GC will keep the right 
count of references so the object will be freed when it executes if no 
longer needed.

regards,

javier ramírez


More information about the Ruby mailing list