Change string to hash with ruby -


i have ugly string looks this:

"\"new\"=>\"0\"" 

which best way converting hash object?

problem "\"new\"=>\"0\"" not hash. first step should manipulate hash:

"{" + + "}" # => "{\"new\"=>\"0\"}" 

now once have hash looking string can convert hash this:

eval "{" + + "}" # => {"new"=>"0"} 

however there still 1 issue, eval is not safe , inadvisable use. lets manipulate string further make json-like , use json.parse:

require `json` json.parse ("{" + + "}").gsub("=>",":") # => {"new"=>"0"} 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -