<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Beyond Syntax &#187; yaml</title>
	<atom:link href="http://www.beyond-syntax.com/tag/yaml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.beyond-syntax.com</link>
	<description>Looking beyond syntactical meaning</description>
	<lastBuildDate>Thu, 01 Jul 2010 22:45:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>If YAML.dump can&#8217;t produce valid YAML&#8230;</title>
		<link>http://www.beyond-syntax.com/2009/02/if-yaml-dump-cant-produce-valid-yaml/</link>
		<comments>http://www.beyond-syntax.com/2009/02/if-yaml-dump-cant-produce-valid-yaml/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 05:12:05 +0000</pubDate>
		<dc:creator>phinze</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[rant]]></category>
		<category><![CDATA[yaml]]></category>

		<guid isPermaLink="false">http://www.beyond-syntax.com/?p=3</guid>
		<description><![CDATA[Yesterday I had fun with ruby&#8217;s YAML module not loading a piece of YAML I needed it to load.  Syntax Error&#8230; fine&#8211;all in a day&#8217;s work. But then I started looking at  the YAML in question and I realized, this was GENERATED by the module itself!

First of all, the reason I was using [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I had fun with ruby&#8217;s YAML module not loading a piece of YAML I needed it to load.  <code>Syntax Error</code>&#8230; fine&#8211;all in a day&#8217;s work. But then I started looking at  the YAML in question and I realized, this was GENERATED by the module itself!</p>
<p><span id="more-3"></span></p>
<p>First of all, the reason I was using YAML was as a quick, simple, human-readable backend for a tiny little side-project.  I chose YAML over SQLite or CSV because neither have the human-readable thing going for them, plus I&#8217;m working in Ruby and it seemed to have nice built-in support for YAML.</p>
<p>You can take any object you like (with obvious exceptions like streams and sockets) and just <code>YAML.dump</code> it, creating a happy string waiting to be written to the nearest file.  So given this class:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Foo
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@bar</span> = <span style="color:#996600;">'one'</span>
    <span style="color:#0066ff; font-weight:bold;">@baz</span> = <span style="color:#006666;">2</span>
    <span style="color:#0066ff; font-weight:bold;">@qux</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">3</span>, <span style="color:#996600;">'four'</span>, <span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>You can do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#006600; font-weight:bold;">&amp;</span>gt; f = Foo.<span style="color:#9900CC;">new</span>
=<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#008000; font-style:italic;">#</span>
<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#9900CC;">dump</span><span style="color:#006600; font-weight:bold;">&#40;</span>f<span style="color:#006600; font-weight:bold;">&#41;</span>
=<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;--- !ruby/object:Foo <span style="color:#000099;">\n</span>bar: one<span style="color:#000099;">\n</span>baz: 2<span style="color:#000099;">\n</span>qux: <span style="color:#000099;">\n</span>- 3<span style="color:#000099;">\n</span>- four<span style="color:#000099;">\n</span>- 5<span style="color:#000099;">\n</span>&quot;</span></pre></div></div>

<p>And that YAML string at the end there looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">--- !ruby/object:Foo
bar: one
baz: 2
qux:
- 3
- four
- 5</pre></div></div>

<p>So you can stuff that wherever you&#8217;d like (<code>file.yml</code> let&#8217;s say) and then at some later date all you need to do is:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#006600; font-weight:bold;">&amp;</span>gt; new_f = <span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#9900CC;">load_file</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'file.yml'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
=<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#008000; font-style:italic;">#</span>
<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#006600; font-weight:bold;">&amp;</span>gt; f == new_f
=<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#0000FF; font-weight:bold;">true</span></pre></div></div>

<p>Fun times, no?  And most importantly fun times that don&#8217;t require one learns YAML in great detail.  Well, let me show you exactly where the fun times ended for me.  For an example, let&#8217;s make a simple hash with our <code>f</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#006600; font-weight:bold;">&amp;</span>gt; h = <span style="color:#006600; font-weight:bold;">&#123;</span> f =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
=<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#008000; font-style:italic;">#=&amp;gt;2}</span>
<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#9900CC;">dump</span><span style="color:#006600; font-weight:bold;">&#40;</span>h<span style="color:#006600; font-weight:bold;">&#41;</span>
=<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;--- <span style="color:#000099;">\n</span>!ruby/object:Foo ? <span style="color:#000099;">\n</span>  bar: one<span style="color:#000099;">\n</span>  baz: 2<span style="color:#000099;">\n</span>  qux: <span style="color:#000099;">\n</span>  - 3<span style="color:#000099;">\n</span>  - four<span style="color:#000099;">\n</span>  - 5<span style="color:#000099;">\n</span>: 2<span style="color:#000099;">\n</span><span style="color:#000099;">\n</span>&quot;</span></pre></div></div>

<p>Seems fine, no?</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">---
!ruby/object:Foo ?
  bar: one
  baz: 2
  qux:
  - 3
  - four
  - 5
: 2</pre></div></div>

<p>So we go about our business, right?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#CC0066; font-weight:bold;">load</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#9900CC;">dump</span><span style="color:#006600; font-weight:bold;">&#40;</span>h<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC00FF; font-weight:bold;">ArgumentError</span>: syntax error on line <span style="color:#006666;">2</span>, col <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span></pre></div></div>

<p>Boo.  So now I have to go and do what I was trying to avoid doing in the first place: learn about YAML.   Well after wading through the schema documents for YAML, which are long, full of BNF, and no fun.  At the end of the day it turns out that <code>YAML.dump</code> produces invalid YAML whenever an object is used as a key in a Hash.  Without going into too much detail (partially because I don&#8217;t fully understand it), the question mark needs to come before the ruby object tag for it to be valid.</p>
<p>Here&#8217;s a workaround that just does some regexp munging on the string :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#CC0066; font-weight:bold;">load</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#9900CC;">dump</span><span style="color:#006600; font-weight:bold;">&#40;</span>h<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#006600; font-weight:bold;">&#40;</span>!ruby.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span> \? <span style="color:#006600; font-weight:bold;">*</span>$<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#996600;">&quot;? #{$1}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
=<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#008000; font-style:italic;">#=&amp;gt;2}</span></pre></div></div>

<p>Looks like I&#8217;ll be submitting my first bug report to rubylang.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.beyond-syntax.com/2009/02/if-yaml-dump-cant-produce-valid-yaml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
