How to change hash keys from `Symbol`s to `String`s?

ruby hash keys to symbols
ruby symbol to string
ruby hash stringify keys
convert key to hash
stringify_keys
ruby string to hash
ruby hash symbol vs string
ruby hash convert to symbol

I am using Ruby on Rails 3.2.2 and I would like to "easily" / "quickly" change hash keys from Symbols to Strings. That is, from {:one => "Value 1", :two => "Value 2", ...} to {"one" => "Value 1", "two" => "Value 2", ...}.

How can I make that by using less code as possible?

simply call stringify_keys (or stringify_keys!)

http://apidock.com/rails/Hash/stringify_keys

Converting Ruby Hash keys to Strings/Symbols · Jamie Tanna , key), Ruby calculates the hash code of the key by calling its hash method. Object#hash to change this, but the only time you should need to do this is if your the slight optimization you can get by using symbols as keys instead of strings. if you try to access the hash above with symbols, you'll get nil myhash[:name] #nil however myhash['name'] # "James" Solution There are two methods that can help you. One is symbolize_keys! which only symbolizes first level keys and withindifferentaccess which symbolizes recursively.

Use stringify_keys/stringify_keys! methods of the Hash class.

You can also use some_hash.with_indifferent_access to return a Hash instance where your key can be specified as symbols or as strings with no difference.

Ruby Cookbook, A symbol literal is written by prefixing an identifier or string with a colon: :symbol # A end You can convert a String to a Symbol using the intern orto_sym methods. For this reason, symbols are generally preferred to strings as hash keys. MD5 hashes are commonly used with smaller strings when storing passwords, credit card numbers or other sensitive data in databases such as the popular MySQL. This tool provides a quick and easy way to encode an MD5 hash from a simple string of up to 256 characters in length. MD5 hashes are also used to ensure the data integrity of files.

stringify_keys is nice, but only available in Rails. Here's how I would do it in a single line, with zero dependencies:

new_hash = Hash[your_hash.collect{|k,v| [k.to_s, v]}]

This works on Ruby 1.8.7 and up. If you are working with Ruby 2.1, you can do:

new_hash = a.collect{|k,v| [k.to_s, v]}.to_h

Note that this solution is not recursive, nor will it handle "duplicate" keys properly. eg. if you have :key and also "key" as keys in your hash, the last one will take precedence and overwrite the first one.

The Ruby Programming Language: Everything You Need to Know, SYMBOLS IN THE Tiger COMPILER The hash table of Program 5.2 must examine every character of the string s for the hash operation, and then again each To avoid unnecessary string comparisons, we can convert each string to a symbol, hash-key is very fast (in case we want to make hash table mapping symbols to  Note that this solution is not recursive, nor will it handle "duplicate" keys properly. eg. if you have :key and also "key" as keys in your hash, the last one will take precedence and overwrite the first one.

stringify_keys from rails

http://api.rubyonrails.org/classes/Hash.html#method-i-stringify_keys

hash = { name: 'Rob', age: '28' }
hash.stringify_keys
# => { "name" => "Rob", "age" => "28" }

Modern Compiler Implementation in C, These two reasons makes symbols as keys faster than strings as keys. Ruby provides methods to convert a symbol to a string and vice versa. Emacs lisp has 2 types of collection of key/value pairs. Association List (aka alist). → a ordered list of key val pairs. Keys may repeat. Hash Table. → unordered set of key val pairs.

there is a nice library that does the trick, the library is "facets/hash/rekey" and the method is rekey!. Se my example below of how to use it. It is just a copy past of

> require 'facets/hash/rekey'
 => true
> a = {:one => "Value 1", :two => "Value 2"}
 => {:one=>"Value 1", :two=>"Value 2"} 
> a.rekey!(&:to_s)
 => {"one"=>"Value 1", "two"=>"Value 2"} 
> a
 => {"one"=>"Value 1", "two"=>"Value 2"}

Hashes and symbols in Ruby - DEV, Returns a new hash with all keys converted to symbols, as long as they respond to to_sym. hash = { 'name' => 'Rob', 'age' => '28' } hash.symbolize_keys  Using strings as symbols for hash keys is a headache for many programmers. (there are a lot of libraries to make this easier). There is a lot of libraries written with the traditional way in mind. Thus all the gems that stores in the same hash strings and symbols could break if we change the exception.

symbolize_keys (Hash), Problem. you have a hash with string keys but want to access them with symbols instead. You have: myhash = {"name" => "James", "email"  A hash table is a data structure used to implement symbol table (associative array), a structure that can map keys to values. — Wikipedia. When a hash table is created, it’s really internally

Convert Ruby hash keys to symbols (Example), A hash in Ruby contains keys with corresponding values like {a: 1, b: 2}. Here the keys Symbols and strings can both be used as keys on hashes. In Rails, both will is mutable. You can't change a symbol once it's created. Online Hash Calculator. Online Hash Calculator lets you calculate the cryptographic hash value of a string or file. Multiple hashing algorithms are supported including MD5, SHA1, SHA2, CRC32 and many other algorithms.

Tips. Ruby for Rails: Hash, What are the differences between symbols, strings & variables? Let's talk You can also use symbols as hash keys. Converting Between Strings & Symbols.

Comments
  • If you used HashWithIndifferentAccess instead of a hash, then that would be done automatically for you.
  • deep_stringify_keys is recursive, but I believe it's only in Rails 4: apidock.com/rails/v4.0.2/Hash/deep_stringify_keys
  • Be careful with this strategy as it will also turn any values that are symbols into strings.