Difference between local and instance variables in ruby

Posted by fflyer05 on Stack Overflow See other posts from Stack Overflow or by fflyer05
Published on 2011-01-13T22:39:19Z Indexed on 2011/01/13 22:53 UTC
Read the original article Hit count: 109

Filed under:

I am working on a script that creates several fairly complex nested hash datastructures and then iterates through them conditionally creating database records. This is a standalone script using active record. After several minutes of running I noticed a significant lag in server responsiveness and discovered that the script, while being set to be nice +19, was enjoying a steady %85 - %90 total server memory.

In this case I am using instance variables simply for readability. It helps knowing what is going to be re-used outside of the loop vs. what won't. Is there a reason to not use instance variables when they are not needed? Are there differences in memory allocation and management between local and instance variables? Would it help setting @variable = nil when its no longer needed?

© Stack Overflow or respective owner

Related posts about ruby