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
Post a Comment