Convert String To Hash Ruby

[Solved] Convert String To Hash Ruby | Ruby - Code Explorer | yomemimo.com
Question : convert string to hash ruby

Answered by : al-arafat-tanin

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('=>', ':')

Source : https://stackoverflow.com/questions/1667630/how-do-i-convert-a-string-object-into-a-hash-object | Last Update : Fri, 17 Sep 21

Question : string to hash ruby

Answered by : excalizan

pairs = string.scan(/(\w+):\s+([^;]+)/).map { |k,v| [k.to_sym, v.strip] }
result = Hash[pairs]

Source : | Last Update : Sat, 23 Apr 22

Answers related to convert string to hash ruby

Code Explorer Popular Question For Ruby