Skip to content

Instantly share code, notes, and snippets.

@davissp14
Created April 25, 2013 20:01
Show Gist options
  • Save davissp14/5462670 to your computer and use it in GitHub Desktop.
Save davissp14/5462670 to your computer and use it in GitHub Desktop.
Multi level symbolizer with tail recursion.
#Example
# Input: {"first1"=>"hi", "first2"=>{"second"=>"gah", "second1"=>{"third1"=>"toot"}, "second2"=>"gah3"}}
# Output: {:first1=>"hi", :first2=>{:second=>"gah", :second1=>{:third1=>"toot"}, :second2=>"gah3"}}
def symbolize(hash)
toprocess = [hash]
while h = toprocess.shift
h.keys.each do |k|
v = h[k]
if v.is_a?(Hash)
toprocess << v
end
if k.is_a?(String)
h.delete(k)
h[k.to_sym] = v
end
end
end
hash
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment