Created
April 25, 2013 20:01
-
-
Save davissp14/5462670 to your computer and use it in GitHub Desktop.
Multi level symbolizer with tail recursion.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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