A collection of computer systems and programming tips that you may find useful.
 
Brought to you by Craic Computing LLC, a bioinformatics consulting company.

Thursday, April 25, 2019

Ruby serializable_hash removes ruby/object from YAML output

I use YAML as a convenient way to serialize data in a number of projects, most of which are written in Ruby and Rails

You generate the YAML representation with the .to_yaml method - really simple.

p = Paper.find(359)
puts p.to_yaml

But if the input is a ruby or rails object the output is prefixed with !ruby/object and that causes problems when you try and load that document in another script that does not know about this object

For example - here is an example of a Paper object from a Rails application.

--- !ruby/object:Paper
attributes:
  id: 359
  pmid: 7945531
  title: 'Pharmacokinetics of a new human monoclonal antibody against cytomegalovirus.
    Third communication: correspondence of the idiotype activity and virus neutralization
    activity of the new monoclonal antibody, regavirumab in rat serum and its pharmacokinetics'
[...]

If I try and read this file in a separate script I get this error because that script has no concept of a Paper object.

y = YAML.load_file('test.yml')
ArgumentError: undefined class/module Paper
from /Users/jones/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/psych-2.0.5/lib/psych/class_loader.rb:53:in `path2class'
from /Users/jones/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/psych-2.0.5/lib/psych/class_loader.rb:53:in `resolve'
from /Users/jones/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/psych-2.0.5/lib/psych/class_loader.rb:45:in `find'
[...]

The way to strip off the ruby/object 'header' is to use serializable_hash before to_yaml.

---
abstract: TI-23 consists of lyophilized regavirumab (monoclonal antibody C23, MCA
[...]
id: 359
pmid: 7945531
publication_date: 1994-07-01
title: 'Pharmacokinetics of a new human monoclonal antibody against cytomegalovirus.
  Third communication: correspondence of the idiotype activity and virus neutralization
  activity of the new monoclonal antibody, regavirumab in rat serum and its pharmacokinetics'

It looks like the keys in the yaml block are output in alphabetical order.

It's a simple fix but I had to hunt around to find it.







8 comments:

tejaswini said...

I think I have never watched such online diaries ever that has absolute things with all nuances which I need. So thoughtfully update this ever for us.
360DigiTMG data science course

360DigiTMG said...

I see the best substance on your blog and I unbelievably love getting them.
hrdf training course

Big Data Analytics Malaysia said...



Simply the manner in which I have anticipated. Your site truly is intriguing. https://360digitmg.com/course/artificial-intelligence-ai-and-deep-learning

dataanalyticscourse said...

Here at this site actually the particular material assortment with the goal that everyone can appreciate a great deal.
360DigiTMG big data training

Ishika4avr said...

Hello guys how are you all hoping you guys get better in Corona time? I know you guys are desperate to meet me because Jaipur escorts are always ready to serve you? I hope you are looking forward to meeting your dream girls.

Kevin Booth said...

Investing in cryptocurrencies can seem like a daunting task, let alone managing and protecting it. One should know the correct use of blockchain wallets to keep their crypto safe. Even a small inattention can lead to big losses. If you are also concerned about the security of your cryptocurrency and need help on how to protect it, feel free to contact us at our blockchain customer service number Our blockchain experts will help you with your request and then you can live with your cryptocurrency without any burden and risk.

360DigiTMGAurangabad said...

Excellence blog! Thanks For Sharing, The information provided by you is really a worthy. I read this blog and I got the more information about
data scientist courses aurangabad

Unknown said...

I share this wedding photography quite a bit so I thought it would be helpful to me as well to have
some of the best leather jacket in one post.

Archive of Tips