Reputation: 29
How convert this string (it's one string from db) to hash with array?
---
fl:
-
- 500.0
price:
- 2162.72
- 2152.72
period:
-
- 3 weeks
I got this data by doing this:
changes_data = box.changes.to_hash
changes_data.each do |key, val|
if val[0].eql? val[1]
changes_data.delete(key)
else
changes_data[key] = val.to_a
end
end
But now I don't know how to convert back. I want get maybe this:
{:fl => ['0', '500.0'], :price=>['2162.72','2152.72'],.......}
or convert to object box
Upvotes: 0
Views: 145
Reputation: 2264
Looks like your data is just YAML. So you can do this:
require 'yaml'
serialized_str = ... # retrieve the serialized string from the database
deserialized_hash = YAML.parse(serialized_str).to_ruby
deserialized_hash.class # => Hash
Upvotes: 1