# YAML - how to encode an empty string

**URL:** <https://www.ruby-forum.com/t/yaml-how-to-encode-an-empty-string/95397>\
**Category:** Ruby\
**Created:** [May 31, 2006, 5:26am UTC](https://www.ruby-forum.com/t/yaml-how-to-encode-an-empty-string/95397 "2006-05-31T05:26:10Z")\
**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:** [May 31, 2006, 5:26am UTC](https://www.ruby-forum.com/t/yaml-how-to-encode-an-empty-string/95397/1 "2006-05-31T05:26:10Z")

</div>

Learned something interesting (to me) today.

In a fixture file I’m using to test some db code, I need a field to  
contain an empty string. I had no idea how to encode that in YAML. I  
checked the faq but it consists of one entry about tabs. 🙂

I also searched this list and the Rails list going back to Jan 2005  
and came up dry.

irb to the rescue:

cremes$ irb  
irb(main):001:0\> require ‘yaml’  
=\> true  
irb(main):002:0\> empty = “”  
=\> “”  
irb(main):003:0\> empty.to\_yaml  
=\> “— “”\n”

I put that into my fixture file (just the “”) and everything is  
peachy. Next time I’ll experiment with irb _first_.

cr

---

<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:** [May 31, 2006, 7:24am UTC](https://www.ruby-forum.com/t/yaml-how-to-encode-an-empty-string/95397/2 "2006-05-31T07:24:40Z")

</div>

On May 30, 2006, at 11:23 PM, removed\_email\_address@domain.invalid wrote:

> cr

and for even quicker checking of stuff like this:  
% ruby -ryaml -e ‘y “”’  
— “”
