0

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

5
  • are you using an ORM to comment to the database (such as ActiveRecord)? Commented Aug 25, 2014 at 16:52
  • I think not. If you have understood correctly. Commented Aug 25, 2014 at 16:59
  • No. I have two objects. They were compared, and the difference is sent to the database in a separate . Commented Aug 25, 2014 at 17:08
  • Yes, it seems you are right. This YML. Sorry, I just started to learn. Commented Aug 25, 2014 at 17:12
  • 1
    Are you using serialize in your model or are you just throwing some random Hash into a string column? Commented Aug 25, 2014 at 17:13

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.