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.







15 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

360digitmgdelhi said...

I think this is an extraordinary article. You make this information interesting and secures. You give perusers a lot to consider and I esteem such a forming.
iot courses in delhi

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

360digitmg said...

It is extremely nice to see the greatest details presented in an easy and understanding manner.
business analytics course

Pooja Sharma said...

Agra Call Girl and Female Model Escort Services in Agra for your spicy erotic desires. Agra Escorts is the best Escorts Girls service in Agra free and paid sex incall and outcall independent agency. If you are interested please contact me for more information visit our website.

Shivani Sharma said...

Aerocity Escorts is one of the top-class agencies that offering a high-class escort service in Aerocity. Aerocity escort service is the best escort service provider and one of the great call girls services. We have all types of profile call girls in Aerocity escorts. If you are interested please call me and for more information visit our website.
Escorts In Aerocity

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.

Bed Pari Bangalore Escorts said...


Thanks for sharing this brilliant article it was a very useful and helpful article.

Bangalore Escorts
Bangalore escorts service
Bangalore escorts
Bangalore Escorts
VIP Escorts Bangalore
Call Girls in Bangalore

Divya Goal said...

Escorts near sterlings mac hotel bangalore
Escorts service near the chancery pavilion Bangalore
Independent Bangalore Escorts
Bangalore Escorts Services
Hifi Escorts
housewife escort

Bed Pari Bangalore Escorts said...

Thanks for sharing this brilliant article it was a very useful and helpful article.

JP Nagar Escorts
Vijaynagar escorts
yelahanka escorts
bellandur escorts
Bangalore Escorts
VIP Escorts Bangalore
Call Girls in Bangalore

Divya Goal said...

Whitefield Escorts
Yelahanka Escorts
Independent Bangalore Escorts
Bangalore Escorts Services
ub city escorts
hubli escorts

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.

Microsoft365.com/setup said...

this is a ayurvedic medicine for early discharge problem
AAO Full Form
best medicine for premature ejaculation in india
best medicine for premature ejaculation in india
best medicine for erectile dysfunction without side effects
natural male enhancement pills, tablets, medicine and capsule
male people ask which tablet is best for enhancement
Powerful Male Enhancement Supplements - Youtube
Male enhancement pills for men near me

Archive of Tips