From: Matt Mencel Date: 2012-01-10T05:58:03+09:00 Subject: Re: Class instance variable question Hi Jesus, I tried your suggestion.... class Tools def self.get_all_values_nested(nested_hash={}) @all_values = _get_all_values_nested(nested_hash) end def self._get_all_values_nested(nested_hash={}, path=[]) all_values = {} nested_hash.each_pair do |k,v| path << k case v when Array, DateTime, FalseClass, Fixnum, NilClass, String, TrueClass then all_values.merge!({"#{path.join(".")}" => "#{v}"}) when Hash then all_values.merge!(_get_all_values_nested(v, path)) else raise ArgumentError, "Unhandled type #{v.class}" end path.pop end return all_values end end ...and it seems to work nicely. The all_values hash is cleared each time, but values in path are sent in each time the _get_all_values_nested is recursively called. I'll see how it goes with further testing. Thanks! Matt ----- Original Message ----- From: "Jesús Gabriel y Galán" To: "ruby-talk ML" Sent: Monday, January 9, 2012 2:14:37 PM Subject: Re: Class instance variable question On Mon, Jan 9, 2012 at 7:46 PM, Stefano Crocco wrote: > 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 The problem there is that you lose the aggregation of path within the recursive calls, since you are clearing at the beggining of each call. > If those two variables are only needed from within the body of > get_all_values_nested, then you should use local variables instead. That's what I thought. If this is not the case, then the solution should be different, but I'm not sure how those two requirements hold up together: you need them empty before calling that method, but need to read the resulting values in other cases. Also, I think path ends up empty after the call, so maybe we are talking only about all_values. If this is the case I would do it like this: class Tools def self.get_all_values_nested(nested_hash={}) @all_values = _get_all_values_nested(nested_hash) end end and use the definition for _get_all_values_nested that I propose in my other email. The OP should clarify, but I don't think his use case involves having memory of the computation within calls or something like that. Jesus.