Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a String object into a Hash object?

Tags:

ruby

I have a string which looks like a hash:

"{ :key_a => { :key_1a => 'value_1a', :key_2a => 'value_2a' }, :key_b => { :key_1b => 'value_1b' } }"

How do I get a Hash out of it? like:

{ :key_a => { :key_1a => 'value_1a', :key_2a => 'value_2a' }, :key_b => { :key_1b => 'value_1b' } }

The string can have any depth of nesting. It has all the properties how a valid Hash is typed in Ruby.

like image 858
Waseem Avatar asked Oct 07 '22 16:10

Waseem


People also ask

How do I hash a string in C#?

Getting the hash code of a string is simple in C#. We use the GetHashCode() method. A hash code is a uniquely identified numerical value. Note that strings that have the same value have the same hash code.

Can you convert hash to string?

Simple answer is you can't. You can however generate tables of values and their hash equivalent and then go searching through those, they're known as rainbow tables, see here on Wikipedia, but depending on how complicated the input value was these become increasingly unfeasible.

How do I convert a string to an array in Ruby?

The general syntax for using the split method is string. split() . The place at which to split the string is specified as an argument to the method. The split substrings will be returned together in an array.


4 Answers

For different string, you can do it without using dangerous eval method:

hash_as_string = "{\"0\"=>{\"answer\"=>\"1\", \"value\"=>\"No\"}, \"1\"=>{\"answer\"=>\"2\", \"value\"=>\"Yes\"}, \"2\"=>{\"answer\"=>\"3\", \"value\"=>\"No\"}, \"3\"=>{\"answer\"=>\"4\", \"value\"=>\"1\"}, \"4\"=>{\"value\"=>\"2\"}, \"5\"=>{\"value\"=>\"3\"}, \"6\"=>{\"value\"=>\"4\"}}"
JSON.parse hash_as_string.gsub('=>', ':')
like image 87
zolter Avatar answered Oct 19 '22 07:10

zolter


Quick and dirty method would be

eval("{ :key_a => { :key_1a => 'value_1a', :key_2a => 'value_2a' }, :key_b => { :key_1b => 'value_1b' } }") 

But it has severe security implications.
It executes whatever it is passed, you must be 110% sure (as in, at least no user input anywhere along the way) it would contain only properly formed hashes or unexpected bugs/horrible creatures from outer space might start popping up.

like image 22
Toms Mikoss Avatar answered Oct 19 '22 07:10

Toms Mikoss


The string created by calling Hash#inspect can be turned back into a hash by calling eval on it. However, this requires the same to be true of all of the objects in the hash.

If I start with the hash {:a => Object.new}, then its string representation is "{:a=>#<Object:0x7f66b65cf4d0>}", and I can't use eval to turn it back into a hash because #<Object:0x7f66b65cf4d0> isn't valid Ruby syntax.

However, if all that's in the hash is strings, symbols, numbers, and arrays, it should work, because those have string representations that are valid Ruby syntax.

like image 28
Ken Bloom Avatar answered Oct 19 '22 05:10

Ken Bloom


I had the same problem. I was storing a hash in Redis. When retrieving that hash, it was a string. I didn't want to call eval(str) because of security concerns. My solution was to save the hash as a json string instead of a ruby hash string. If you have the option, using json is easier.

  redis.set(key, ruby_hash.to_json)
  JSON.parse(redis.get(key))

TL;DR: use to_json and JSON.parse

like image 42
Jared Menard Avatar answered Oct 19 '22 06:10

Jared Menard