From: "Jesús Gabriel y Galán" Date: 2012-01-10T06:50:45+09:00 Subject: Re: Class instance variable question On Mon, Jan 9, 2012 at 9:58 PM, Matt Mencel wrote: > 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. But, do you really need a class instance variable holding the result of a call? If not, please considering droping the instance variable stuff altogether. On the other hand, I don't understand what you are saying here, sorry. What do you mean by "values in path are sent in"? Also the "but" in front of that sentence seems to imply that something is wrong? In each iteration through the hash entries, path contains the all keys that lead nested hash through nested hash to the current key under inspection. If the key is a hash, the path gets passed to the recursive call, so that in that recursive call, each key has the previous path at the start of the array and can append to the end of the array each current key. Jesus.