<?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>SmartLogic Solutions Blog &#187; YAML</title>
	<atom:link href="http://blog.smartlogicsolutions.com/category/yaml/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.smartlogicsolutions.com</link>
	<description>News and updates from the people at SmartLogic Solutions</description>
	<lastBuildDate>Tue, 30 Nov 2010 21:39:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Aptinstaller 0.3.2 Released</title>
		<link>http://blog.smartlogicsolutions.com/2009/01/14/aptinstaller-032-released/</link>
		<comments>http://blog.smartlogicsolutions.com/2009/01/14/aptinstaller-032-released/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 21:39:53 +0000</pubDate>
		<dc:creator>Nick Gauthier</dc:creator>
				<category><![CDATA[Nick Gauthier]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[YAML]]></category>

		<guid isPermaLink="false">http://blog.smartlogicsolutions.com/?p=472</guid>
		<description><![CDATA[Do you program ruby on rails? Do you use geminstaller? Do you run Ubuntu? If you want to pull it all together, read on! Aptinstaller is heavily influenced by geminstaller. Geminstaller lets you write some YAML like this: 1 --- 2 defaults: 3   install_options: --no-ri --no-rdoc 4 gems: 5 - name: rails 6   version: '= [...]]]></description>
			<content:encoded><![CDATA[<p>Do you program ruby on rails? Do you use geminstaller? Do you run Ubuntu?</p>
<p>If you want to pull it all together, read on!</p>
<p><span id="more-472"></span></p>
<p><a href="http://github.com/ngauthier/aptinstaller">Aptinstaller</a> is heavily influenced by geminstaller. <a href="http://geminstaller.rubyforge.org/">Geminstaller</a> lets you write some YAML like this:<br />
<code><br />
1 ---<br />
2 defaults:<br />
3   install_options: --no-ri --no-rdoc<br />
4 gems:<br />
5 - name: rails<br />
6   version: '= 2.2.2'<br />
7 - name: rubyist-aasm<br />
8   version: '= 2.0.5'<br />
</code><br />
And then your project will check those dependencies when the environment loads to ensure those gems are loaded. Then, you can run &#8220;geminstaller&#8221; from the root of your project and all those gems will be automatically installed.</p>
<p>Aptinstaller does the same thing, but it uses Apt, which is the <a href="http://www.debian.org">Debian</a> package manager found on the popular linux distribution <a href="http://www.ubuntu.com">Ubuntu</a>.</p>
<p>You can write YAML like this:<br />
<code><br />
  1 ---<br />
  2 packages:<br />
  3   - name: firefox<br />
  4   - name: gnome-terminal<br />
</code><br />
And it will ensure those packages are installed. If they aren&#8217;t, you can install them automatically by running &#8220;aptinstaller&#8221; from the root of your project (or by passing it the path).</p>
<p>Aptinstaller is very basic at the moment, but I plan on adding version support and the ability to pass on instructions to the user if they need to build the package themselves. Also, it would be great to include alternate package names for yum, but that may be for a separate gem.</p>
<p>Please let me know if you have any suggestions.</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.smartlogicsolutions.com/2010/02/01/setting-up-ubuntu-9-10-for-ruby-on-rails-development/" rel="bookmark" class="crp_title">Setting Up Ubuntu 9.10 for Ruby On Rails Development</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/06/06/deploying-rails-apps-with-capistrano-without-root-or-sudo-privileges/" rel="bookmark" class="crp_title">Deploying Rails Apps with Capistrano without root or sudo Privileges</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/10/08/benchmark-ruby-code-with-r-rsruby-and-better-benchmark/" rel="bookmark" class="crp_title">Benchmark Ruby Code with R, rsruby and better-benchmark</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/07/07/i-cant-upgrade-rubygems-from-111-to-120-on-ubuntu/" rel="bookmark" class="crp_title">I can&#8217;t upgrade RubyGems from 1.1.1 to 1.2.0 on Ubuntu</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/06/10/shell-script-to-upgrade-ruby-enterprise-edition-while-maintaining-directory-naming-sanity/" rel="bookmark" class="crp_title">Shell Script to Upgrade Ruby Enterprise Edition while Maintaining Directory Naming Sanity</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.smartlogicsolutions.com/2009/01/14/aptinstaller-032-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ruby: Patch to fix broken YAML.dump for multi-line strings (String#to_yaml)</title>
		<link>http://blog.smartlogicsolutions.com/2008/09/04/ruby-patch-to-fix-broken-yamldump-for-multi-line-strings-stringto_yaml/</link>
		<comments>http://blog.smartlogicsolutions.com/2008/09/04/ruby-patch-to-fix-broken-yamldump-for-multi-line-strings-stringto_yaml/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 21:35:40 +0000</pubDate>
		<dc:creator>John Trupiano</dc:creator>
				<category><![CDATA[John Trupiano]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[YAML]]></category>

		<guid isPermaLink="false">http://blog.smartlogicsolutions.com/?p=213</guid>
		<description><![CDATA[I love YAML. It&#8217;s portable. The majority of languages I work with already have libraries built to read/write it. It&#8217;s more lightweight, more expressive, and easier to read than XML. I really love YAML. Unfortunately, it turns out that Ruby&#8217;s YAML library is a little incomplete for some edge cases. I just discovered that YAML.dump [...]]]></description>
			<content:encoded><![CDATA[<p>I love YAML.  It&#8217;s portable.  The majority of languages I work with already have libraries built to read/write it.  It&#8217;s more lightweight, more expressive, and easier to read than XML.  I really love YAML.</p>
<p>Unfortunately, it turns out that Ruby&#8217;s YAML library is a little incomplete for some edge cases. I just discovered that YAML.dump fails to generate valid YAML for certain multi-line strings.  The specifics are very difficult to explain, and we&#8217;ll summarize them at the end after trying out some examples.  Fire up irb and give the following a try.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'yaml'</span>
s = <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>  Something embedded.<span style="color:#000099;">\n</span>And on a new line.&quot;</span>
<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>s<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p><span id="more-213"></span></p>
<p>You&#8217;ll receive the following error (ruby 1.8.6 (2007-09-24 patchlevel 111) &#8212; yes I know this version is unpatched)</p>

<div class="wp_syntax"><div class="code"><pre class="irb" style="font-family:monospace;">ArgumentError: syntax error on line 3, col 0: `And on a new line.'
	from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/yaml.rb:133:in `load'
	from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/yaml.rb:133:in `load'
	from (irb):73
	from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rational.rb:320</pre></div></div>

<p>Note that the same problem exists in the latest patched ruby enterprise edition (ruby 1.8.6 (2008-08-08 patchlevel 286)).</p>

<div class="wp_syntax"><div class="code"><pre class="irb" style="font-family:monospace;">ArgumentError: syntax error on line 3, col 0: `And on a new line.'
	from /opt/ruby-enterprise-1.8.6-20080810/lib/ruby/1.8/yaml.rb:133:in `load'
	from /opt/ruby-enterprise-1.8.6-20080810/lib/ruby/1.8/yaml.rb:133:in `load'
	from (irb):3</pre></div></div>

<p>So, what the heck is going on here?  I mean, this isn&#8217;t a very complex string by any stretch of the imagination.  Let&#8217;s try a few other variations in irb:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'yaml'</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> s1 = <span style="color:#996600;">&quot;  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <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>s1<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> 
?<span style="color:#006600; font-weight:bold;">&gt;</span>
?<span style="color:#006600; font-weight:bold;">&gt;</span> s2 = <span style="color:#996600;">&quot;  <span style="color:#000099;">\n</span>  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;  <span style="color:#000099;">\n</span>  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <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>s2<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;  <span style="color:#000099;">\n</span>  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>
?<span style="color:#006600; font-weight:bold;">&gt;</span> 
?<span style="color:#006600; font-weight:bold;">&gt;</span> s3 = <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <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>s3<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;">3</span>, col <span style="color:#006666;">0</span>: <span style="color:#996600;">`No indent'
	from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/yaml.rb:133:in `</span><span style="color:#CC0066; font-weight:bold;">load</span><span style="color:#996600;">'
	from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/yaml.rb:133:in `load'</span>
	from <span style="color:#006600; font-weight:bold;">&#40;</span>irb<span style="color:#006600; font-weight:bold;">&#41;</span>:<span style="color:#006666;">9</span>
	from <span style="color:#006600; font-weight:bold;">/</span><span style="color:#CC0066; font-weight:bold;">System</span><span style="color:#006600; font-weight:bold;">/</span>Library<span style="color:#006600; font-weight:bold;">/</span>Frameworks<span style="color:#006600; font-weight:bold;">/</span>Ruby.<span style="color:#9900CC;">framework</span><span style="color:#006600; font-weight:bold;">/</span>Versions<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>usr<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>ruby<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>rational.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">323</span></pre></div></div>

<p>OK, so it appears that our string has to start with a newline for the error to be triggered.  Let&#8217;s look at the YAML created by YAML.dump for each of these 3 strings:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'yaml'</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> s1 = <span style="color:#996600;">&quot;  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</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>s1<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;--- <span style="color:#000099;">\&quot;</span>  Do I work?<span style="color:#000099;">\\</span>n<span style="color:#000099;">\\</span><span style="color:#000099;">\n</span>No indent<span style="color:#000099;">\&quot;</span><span style="color:#000099;">\n</span>&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>
?<span style="color:#006600; font-weight:bold;">&gt;</span> 
?<span style="color:#006600; font-weight:bold;">&gt;</span> s2 = <span style="color:#996600;">&quot;  <span style="color:#000099;">\n</span>  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;  <span style="color:#000099;">\n</span>  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</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>s2<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;--- <span style="color:#000099;">\&quot;</span>  <span style="color:#000099;">\\</span>n  Do I work?<span style="color:#000099;">\\</span>n<span style="color:#000099;">\\</span><span style="color:#000099;">\n</span>No indent<span style="color:#000099;">\&quot;</span><span style="color:#000099;">\n</span>&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>
?<span style="color:#006600; font-weight:bold;">&gt;</span> 
?<span style="color:#006600; font-weight:bold;">&gt;</span> s3 = <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</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>s3<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;--- |-<span style="color:#000099;">\n</span><span style="color:#000099;">\n</span>  Do I work?<span style="color:#000099;">\n</span>No indent<span style="color:#000099;">\n</span>&quot;</span></pre></div></div>

<p>Notice that the actual YAML produced differs substantially from s1&#038;s2 to s3.  Both s1 and s2 encode their values as strings by wrapping the values in quotes.  s3, however, is encoded using the <a href="http://yaml4r.sourceforge.net/cookbook/#three_trailing_newlines_in_literals">|- indicator</a>.  Let&#8217;s take a look at some <a href="http://yaml4r.sourceforge.net/cookbook/#blocks">YAML documentation</a> to better understand what&#8217;s going on.</p>
<div id="attachment_215" class="wp-caption alignnone" style="width: 310px"><a href="http://blog.smartlogicsolutions.com/wp-content/uploads/2008/09/picture-1.png"><img src="http://blog.smartlogicsolutions.com/wp-content/uploads/2008/09/picture-1-300x208.png" alt="Description of multi-line formatting in YAML" title="YAML for Ruby Cookbook" width="300" height="208" class="size-medium wp-image-215" /></a><p class="wp-caption-text">Description of multi-line formatting in YAML</p></div>
<p>The Blocks section defines how multi-line strings (using the | indicator) operate.  It&#8217;s not crystal clear, but ultimately from reading this whole section, I&#8217;ve gathered that YAML requires that whatever block width (the spaces padding the first row) is used in that first row must be maintained for all successive new lines.  In our failing example (s3), our first line with content started with 2 spaces.  As such, when the second line was encountered and characters were present prior to the two spaces, YAML treated it as a separate attribute, throwing an error because it wasn&#8217;t encoded as an attribute (in other words, it didn&#8217;t look like: some_other_attribute: some_value).</p>
<p>So, as promised, let&#8217;s define the characteristics of a string that appear to confuse the ruby YAML library (note that this is probably incomplete and possibly even incorrect, as I&#8217;ve only spent a few hours with this):</p>
<ul>
<li>The string begins with a newline character</li>
<li>The first non-empty line begins with an indent (spaces, not tabs&#8230;this is YAML)</li>
<li>At least one successive line begins without this same indent</li>
</ul>
<p>Great&#8230;..so where&#8217;s the patch?  This is how I got around this problem:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'yaml'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">String</span>
  <span style="color:#9966CC; font-weight:bold;">alias</span> <span style="color:#ff3333; font-weight:bold;">:old_to_yaml</span> <span style="color:#ff3333; font-weight:bold;">:to_yaml</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> to_yaml<span style="color:#006600; font-weight:bold;">&#40;</span>options=<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    new_str = <span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{self}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    new_str = <span style="color:#996600;">&quot; #{new_str}&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> new_str =~ <span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#006600; font-weight:bold;">&#91;</span>\n\r<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">/</span>
    new_str.<span style="color:#9900CC;">old_to_yaml</span><span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I want to point out that I think this patch is potentially <b>very dangerous</b>.  I really don&#8217;t recommend using it unless you absolutely have to, and you&#8217;re ok with actually changing the value of your string.  If you could even isolate it further up the line (by not overriding String#to_yaml), I&#8217;d definitely recommend that.</p>
<p>For my situation, there&#8217;s a few reasons why this is a suitable solution.  First, my problem strings (which come from external sources) are deep within an object hierarchy that I&#8217;m converting to YAML (not just a simple string like this example).  Furthermore, I&#8217;m dealing with html content, so adding an extra space at the front has no effect whatsoever.  Lastly, this is being used by an external and isolated script, so the overriding of String#to_yaml is done on a very localized level.  I would <u>never</u> override String#to_yaml in this fashion in a rails app.</p>
<p>Just for fun (and to prove it works), let&#8217;s run that through irb:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> s3 = <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</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>s3<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;--- |-<span style="color:#000099;">\n</span><span style="color:#000099;">\n</span>  Do I work?<span style="color:#000099;">\n</span>No indent<span style="color:#000099;">\n</span>&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">String</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>   <span style="color:#9966CC; font-weight:bold;">alias</span> <span style="color:#ff3333; font-weight:bold;">:old_to_yaml</span> <span style="color:#ff3333; font-weight:bold;">:to_yaml</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>   <span style="color:#9966CC; font-weight:bold;">def</span> to_yaml<span style="color:#006600; font-weight:bold;">&#40;</span>options=<span style="color:#006600; font-weight:bold;">&#123;</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;">&gt;&gt;</span>     new_str = <span style="color:#CC0066; font-weight:bold;">String</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{self}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>     new_str = <span style="color:#996600;">&quot; #{new_str}&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> new_str =~ <span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#006600; font-weight:bold;">&#91;</span>\n\r<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">/</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>     new_str.<span style="color:#9900CC;">old_to_yaml</span><span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</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>s3<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;--- <span style="color:#000099;">\&quot;</span> <span style="color:#000099;">\\</span>n  Do I work?<span style="color:#000099;">\\</span>n<span style="color:#000099;">\\</span><span style="color:#000099;">\n</span>No indent<span style="color:#000099;">\&quot;</span><span style="color:#000099;">\n</span>&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <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>s3<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot; <span style="color:#000099;">\n</span>  Do I work?<span style="color:#000099;">\n</span>No indent&quot;</span></pre></div></div>

<p>Note that by inserting a space prior to the initial newline, the YAML created is no longer using the |- format, but rather is enclosed by quotes.</p>
<p>I&#8217;ve got to admit that I still don&#8217;t fully understand the situation here.  If any readers have further insights, I would love to hear from you, because my workaround still leaves me a little uncomfortable.  I was also fairly disconcerted to not be able to really find anyone else running into this problem when googling around.  The closest thing I found was a <a href="http://www.google.com/url?sa=t&#038;source=web&#038;ct=res&#038;cd=1&#038;url=http%3A%2F%2Fjira.codehaus.org%2Fbrowse%2FJRUBY-1220&#038;ei=YCHASJL_A5y0iAHdyeD6Dg&#038;usg=AFQjCNEZ27VkDMuLYkAiCOC7DSMMiHy2nA&#038;sig2=FbF5FyZ5bzau5ZrZQkc0rg">bug in JRuby</a> that at first looked promising, but turned out to be something else altogether.</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.smartlogicsolutions.com/2008/10/08/benchmark-ruby-code-with-r-rsruby-and-better-benchmark/" rel="bookmark" class="crp_title">Benchmark Ruby Code with R, rsruby and better-benchmark</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/01/14/aptinstaller-032-released/" rel="bookmark" class="crp_title">Aptinstaller 0.3.2 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/02/13/aasm-interning-empty-string-error/" rel="bookmark" class="crp_title">AASM + interning empty string error</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/06/15/automating-flex-compilation-using-ant/" rel="bookmark" class="crp_title">Automating Flex Compilation Using ANT</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/05/26/including-external-rake-files-in-your-projects-rakefile-keep-your-rake-tasks-organized/" rel="bookmark" class="crp_title">Including external .rake files in your project&#8217;s Rakefile &#8212; keep your rake tasks organized!</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.smartlogicsolutions.com/2008/09/04/ruby-patch-to-fix-broken-yamldump-for-multi-line-strings-stringto_yaml/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

