From: Phrogz Date: 2007-02-09T04:55:08+09:00 Subject: Re: Help optimizing On Feb 8, 12:42 pm, Luke Ivers wrote: > I'm going to cross-post this from the Rails group, because some of the > people here are Ruby ninjas and don't read that forum, and I'd like help > getting this function optimized... its results are useful in a Rails > perspective, but it's functionality has nothing to do with Rails at all. > > class Hash > def to_params(parent = '') > ret = '' > self.keys.each do |key| > if self[key].is_a? Hash > if parent == '' > ret += self[key].to_uri(key.to_s) > else > ret += self[key].to_uri(parent + "[#{key.to_s}]") > end > else > if parent == '' > ret += "#{key}=#{self[key]}&" > else > ret += "#{parent}[#{key}]=#{self[key]}&" > end > end > end > return ret.chomp('&') > end > end I realize the code is there, but I'm having trouble figuring out what it's used for. Could you describe what 'parent' is, and what it means to have nested hashes? One guess I have at improving the speed it to change: self.keys.each{ |key| # repeated function calls needed for self[key] } to self.each_pair{ |key,value| # direct references to value }