user3834491
user3834491

Reputation: 29

How convert string to hash with array?

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

Answers (1)

Nathan Wallace
Nathan Wallace

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

Ruby Docs

Upvotes: 1

Related Questions