Search code examples
rubyyamlmiddleman

How do I YAML dump without the class information?


I am dumping an array of Test class objects, and my YAML is showing this:

---
- !ruby/object:Test
  id: rec1NIfdJz
- !ruby/object:Test
  id: rec1R9TtHF

The problem arises when I use Middleman and then it throws an error for YAML parsing.

YAML Exception parsing ... undefined class/module Test


Solution

  • You need to reduce it to some kind of plain Ruby structure like a Hash first, like:

    YAML.dump(object.to_h)
    

    If you have, or could implement a simple .to_h method.

    YAML, like Marshal, will try and preserve the Ruby class represented by that object. Neutral forms like JSON don't, so that could be an alternative.