From: "Simon Kröger" Date: 2007-02-09T08:25:06+09:00 Subject: Re: Help optimizing Luke Ivers wrote: > Here's the results: > > user system total real > 5.132000 0.000000 5.132000 ( 5.174000) #original > 5.803000 0.000000 5.803000 ( 5.844000) #with inject > 4.868000 0.000000 4.868000 ( 4.880000) #split into two functions > > Can anyone do better than the last one? That wasn't fair at all (you know i couldn't resist, do you?) :) -------------------------------------------------------------------------- def to_params4() result = '' stack = [] each do |key, value| Hash === value ? stack << [key, value] : result << "#{key}=#{value}&" end stack.each do |parent, hash| hash.each do |key, value| if Hash === value stack << ["#{parent}[#{key}]", value] else result << "#{parent}[#{key}]=#{value}&" end end end result.chop end -------------------------------------------------------------------------- as you can see i unrolled the recursion, the benefit isn't that high but it was fun to code. (and yes, i know i shouldn't modify an array i'm iterating over, but hey it seems to work (appending seems to be fine)) cheers Simon