Skip to content

Instantly share code, notes, and snippets.

@duncanbeevers
Forked from Sutto/gist:5471
Created February 9, 2010 15:03
Show Gist options
  • Save duncanbeevers/299285 to your computer and use it in GitHub Desktop.
Save duncanbeevers/299285 to your computer and use it in GitHub Desktop.
class Hash
def recursively_stringify_keys
recursively_xify_keys(:to_s, :recursively_stringify_keys)
end
def recursively_symbolize_keys
recursively_xify_keys(:to_sym, :recursively_symbolize_keys)
end
private
def recursively_xify_keys x, recursion
inject({}) do |a, (k, v)|
a[k.respond_to?(x) ? k.send(x) : k] = v.respond_to?(recursion) ?
v.send(recursion) : v
a
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment