# YAML to anchor same strings in dumps

**URL:** <https://www.ruby-forum.com/t/yaml-to-anchor-same-strings-in-dumps/158997>\
**Category:** Ruby\
**Created:** [January 30, 2009, 6:34am UTC](https://www.ruby-forum.com/t/yaml-to-anchor-same-strings-in-dumps/158997 "2009-01-30T06:34:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [January 30, 2009, 6:34am UTC](https://www.ruby-forum.com/t/yaml-to-anchor-same-strings-in-dumps/158997/1 "2009-01-30T06:34:54Z")

</div>

Hi, there

Is there any way to make YAML alias same strings in dumps? The following  
code fragment:

require ‘yaml’  
s = ‘a’ \* 10  
puts [s, s].to\_yaml

## Produces this:

- aaaaaaaaaa
- aaaaaaaaaa

## While I need:

- &id001 aaaaaaaaaa
- \*id001

Interestingly, if I define s as:  
s = [‘a’ \* 10]

## The output is aliased just fine:

- &id001
  - aaaaaaaaaa

- \*id001

Thanks,  
Gennady.

---

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [January 30, 2009, 11:49pm UTC](https://www.ruby-forum.com/t/yaml-to-anchor-same-strings-in-dumps/158997/2 "2009-01-30T23:49:39Z")

</div>

> * * *
> 
> - &id001 aaaaaaaaaa
> - \*id001

Found a solution:

module ComplexYamlObject  
def is\_complex\_yaml?  
true  
end  
end

s = ‘a’ \* 10  
s.extend ComplexYamlObject  
puts [s, s].to\_yaml

Then the output will be just as I originally wanted:

* * *

- &id001 aaaaaaaaaa
- \*id001

Thought somebody might find it useful,  
Gennady.
