Skip to content

Instantly share code, notes, and snippets.

@constdrop
Last active March 28, 2017 02:54
Show Gist options
  • Save constdrop/4e02775e91b6567c386dd2017f857428 to your computer and use it in GitHub Desktop.
Save constdrop/4e02775e91b6567c386dd2017f857428 to your computer and use it in GitHub Desktop.
Changing ruby hash keys to symbol.
class Hash
def keys_to_sym
self.each_with_object({}) do |(k, v), h|
h[k.to_sym] = v.class == Hash ? v.keys_to_sym : v
end
end
end
hash = {"a" => 1, "b" => 2, "c" => {"A" => "foo", "B" => "bar"}}
hash.keys_to_sym
# => {:a=>1, :b=>2, :c=>{:A=>"foo", :B=>"bar"}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment