Skip to content

Instantly share code, notes, and snippets.

@jnjcub
Created April 22, 2013 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnjcub/5438930 to your computer and use it in GitHub Desktop.
Save jnjcub/5438930 to your computer and use it in GitHub Desktop.
recursive_stringify_keys! on Hash
class Hash
def recursive_stringify_keys!
self.each do |key, value|
string_key = key.to_s
unless key.is_a?(String)
self[string_key] = self[key]
self.delete(key)
if self[string_key].is_a?(Hash)
self[string_key] = self[key.to_s].recursive_stringify_keys!
elsif self[string_key].is_a?(Array) #this can be extended to any collection, reverted back because String had :each !
self[string_key] = self[string_key].collect! { |val| val.respond_to?(:recursive_stringify_keys!) ? val.recursive_stringify_keys! : val }
end
end
end
self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment