From: Stefano Crocco Date: 2012-01-10T03:46:24+09:00 Subject: Re: Class instance variable question Il giorno Tue, 10 Jan 2012 03:26:19 +0900 Matt Mencel ha scritto: > Hi, > > I have this class (below) that takes a deeply nested hash and returns back > a new hash without all the nesting. The problem I'm having is that it > seems to be that @all_values is not empty if I call it a second time from > my program. First time is fine, but the next time I use > get_all_values_nested, @all_values still has the results in it from the > first time it was used. > > Any thoughts on what I'm doing wrong? I just want @path and @all_values to > be empty each time I use the get_all_values_nested function. By definition, instance variables (including instance variables of class objects) keep their values until something else is assigned to them. If you need to access the values stored in @path and @all_values from somewhere other than gel_all_values_nested, but you want them empty in that method, then you'll have to do so yourself: class Tools def self.get_all_values_nested nested_hash = {} @path = [] @all_values = {} # ... end end If those two variables are only needed from within the body of get_all_values_nested, then you should use local variables instead. I hope this helps Stefano