{"id":146,"date":"2009-08-19T15:55:06","date_gmt":"2009-08-19T14:55:06","guid":{"rendered":"http:\/\/www.any-where.de\/blog\/?p=146"},"modified":"2010-05-15T11:45:35","modified_gmt":"2010-05-15T10:45:35","slug":"ruby-hash-convert-string-keys-to-symbols","status":"publish","type":"post","link":"https:\/\/www.any-where.de\/blog\/ruby-hash-convert-string-keys-to-symbols\/","title":{"rendered":"Ruby Hash: Convert String keys to Symbols"},"content":{"rendered":"<p>Ever had a hash which contained strings as keys and needed symbols instead? I do: From a REXML::Document I created a hash. Unfortunately, the keys were all strings and I needed them to be symbols at some other point in the app. Thank god, Ruby is capable of extending any object &#8220;on the fly&#8221;. So, I wrote a simple extension to Hash, which you might find useful as well:<\/p>\n<pre>class Hash\r\n  #take keys of hash and transform those to a symbols\r\n  def self.transform_keys_to_symbols(value)\r\n    return value if not value.is_a?(Hash)\r\n    hash = value.inject({}){|memo,(k,v)| memo[k.to_sym] = Hash.transform_keys_to_symbols(v); memo}\r\n    return hash\r\n  end\r\nend\r\n<\/pre>\n<p>Usage is:<\/p>\n<pre>a = { \"key\" =&gt; 123, \"inner_hash\" =&gt; { \"another_key\" =&gt; \"blabla\" }}\r\nHash.transform_keys_to_symbols(a)\r\n#returns\r\na = { :key =&gt; 123, :inner_hash =&gt; { :another_key =&gt; \"blabla\" }}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ever had a hash which contained strings as keys and needed symbols instead? I do: From a REXML::Document I created a hash. Unfortunately, the keys were all strings and I needed them to be symbols at some other point in the app. Thank god, Ruby is capable of extending any object &#8220;on the fly&#8221;. So, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.any-where.de\/blog\/ruby-hash-convert-string-keys-to-symbols\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Ruby Hash: Convert String keys to Symbols&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,25],"tags":[],"class_list":["post-146","post","type-post","status-publish","format-standard","hentry","category-programming-the-web","category-ruby-on-rails"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts\/146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/comments?post=146"}],"version-history":[{"count":5,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts\/146\/revisions"}],"predecessor-version":[{"id":411,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts\/146\/revisions\/411"}],"wp:attachment":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/media?parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/categories?post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/tags?post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}