# YAML problem with cyclic data structures

**URL:** <https://www.ruby-forum.com/t/yaml-problem-with-cyclic-data-structures/78569>\
**Category:** Ruby\
**Created:** [December 6, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/yaml-problem-with-cyclic-data-structures/78569 "2006-12-06T23:00:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 6, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/yaml-problem-with-cyclic-data-structures/78569/1 "2006-12-06T23:00:43Z")

</div>

I’m having a problem with YAML in the first slightly-more-than-toy  
application that I’ve writing with it. Oddly, when I searched on “ruby  
yaml cyclic” - the first phrase I thought of to get help - I found that  
someone had written a program almost exactly like mine a year ago and  
had the same problem:

> > > > > >

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/150930](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/150930)

I have a program that is trying to use YAML to save out its data,  
without much success.  
In the program, several of the classes have cyclic dependancies.  
To be more specific, the program deals with “rooms” and “connections”  
between the rooms, with each room and connection being represented as a  
class.  
Each room has up to 8 possible connections and each connection  
contains, in turn, a reference to each of the two rooms it connects.  
When YAML saves out its data for extremely simple graphs (2 or 3 rooms  
interconnected), YAML spits out everything fine, and handles the cyclic  
connections using ids to represent the same objects.  
Problem seems to be that this is not working appropiately as the graph  
grows in complexity (even 16 or so rooms can create issues). YAML just  
hangs when saving out the file or, at least, it takes exponentially  
much, much, much longer to save the scene (I’m taking minutes vs. less  
than a second for the 3 rooms).

Is this how it should be? It seems like a bug in the YAML library to  
me.  
\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<

He (or she) never got an answer.

The only things that I can add are:

1. That my Rooms and Doors are “owned” by references from a Floor  
object and have only WeakRefs to each other

2. Adding a single extra Door can increase YAML’s save time from  
several seconds to longer than the 20 minutes I was willing to wait

3. YAML seems quite happy coping with rooms arranged in a tree  
structure, but a very few cycle inducing connections - literally 1 to 3

- will drive it crazy.

1. I’ve looked at my data structures very hard to make sure that they  
are kosher. Or possibly halal.

Comments? I can think of ways around this quite easily for my program,  
but I’d like to know what YAMLs limitations are, or if I might be doing  
something wrong.

Btw - it seems odd that the Ruby YAML Cookbook doesn’t contain the word  
“cyclic”…

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 7, 2006, 7:56pm UTC](https://www.ruby-forum.com/t/yaml-problem-with-cyclic-data-structures/78569/2 "2006-12-07T19:56:25Z")

</div>

Ok…

And now that I stripped WeakRefs from my code (Matz just confirmed I  
found a bug regarding how they work with Marshal) the code Marshals  
perfectly. But YAML still doesn’t work.

If anyone wants me to, I can knock together a stripped down version of  
my code that should still show the problem.

If there are no responses here, then in a couple of days I’ll mail the  
library author.
