# Line breaks in YAML

**URL:** <https://www.ruby-forum.com/t/line-breaks-in-yaml/127398>\
**Category:** Ruby\
**Created:** [January 28, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/line-breaks-in-yaml/127398 "2008-01-28T22:16:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![keith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f05b48/32.png) [@keith](https://www.ruby-forum.com/u/keith)\
**Post date:** [January 28, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/line-breaks-in-yaml/127398/1 "2008-01-28T22:16:13Z")

</div>

Sorry for all the YAML questions today!

Here is my YAML file-

foo:  
this is a test  
to see if ruby  
recognizes line breaks

Here is my Ruby-

Loading development environment (Rails 2.0.2)

> > require ‘yaml’  
> > =\> []
> 
> > test = YAML::load\_file(“test.yml”)  
> > =\> {“foo”=\>“this is a test to see if ruby recognizes line breaks”}
> 
> > puts test[‘foo’]  
> > this is a test to see if ruby recognizes line breaks  
> > =\> nil
> 
> >

Where’d the line breaks go?

---

<div class="post-metadata">

**Author:** ![keith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f05b48/32.png) [@keith](https://www.ruby-forum.com/u/keith)\
**Post date:** [January 28, 2008, 10:59pm UTC](https://www.ruby-forum.com/t/line-breaks-in-yaml/127398/2 "2008-01-28T22:59:17Z")

</div>

Shameless bumb. Stuck at work.

---

<div class="post-metadata">

**Author:** ![keith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f05b48/32.png) [@keith](https://www.ruby-forum.com/u/keith)\
**Post date:** [January 28, 2008, 11:19pm UTC](https://www.ruby-forum.com/t/line-breaks-in-yaml/127398/3 "2008-01-28T23:19:02Z")

</div>

On Jan 28, 2008 10:59 PM, Keith C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Shameless bumb. Stuck at work.

I’ve tried this in irb:

require ‘yaml’

foo = “foo\nbar\nbaz”  
y = YAML::dump(foo)  
#=\> “— |-\nfoo\nbar\nbaz\n”  
foz = YAML::load(y)  
foo == foz #=\> true

Not sure if that’s what you meant.

---

<div class="post-metadata">

**Author:** ![keith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f05b48/32.png) [@keith](https://www.ruby-forum.com/u/keith)\
**Post date:** [January 28, 2008, 11:39pm UTC](https://www.ruby-forum.com/t/line-breaks-in-yaml/127398/4 "2008-01-28T23:39:27Z")

</div>

Thomas W. wrote:

> Not sure if that’s what you meant.

No, that’s not quite it.

Really, what I’d like, is to be able to put multi-line Ruby code  
snippets into a yml file.

For example, my yml file-

source\_id:  
a=2  
b=2  
if (a==b)  
c=3  
end

And my ruby-

Loading development environment (Rails 2.0.2)

> > require ‘yaml’  
> > =\> []
> 
> > test = YAML::load\_file(“test.yml”)  
> > =\> {“source”=\>“a=2 b=2 if (a==b) c=3 end”}
> 
> > eval(test[‘source’])  
> > SyntaxError: (eval):1:in `irb\_binding’: compile error  
> > (eval):1: syntax error, unexpected tIDENTIFIER, expecting $end  
> > a=2 b=2 if (a==b) c=3 end  
> > ^  
> > from (irb):3  
> > from (irb):3
> 
> >

---

<div class="post-metadata">

**Author:** ![keith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f05b48/32.png) [@keith](https://www.ruby-forum.com/u/keith)\
**Post date:** [January 29, 2008, 12:07am UTC](https://www.ruby-forum.com/t/line-breaks-in-yaml/127398/5 "2008-01-29T00:07:15Z")

</div>

On Jan 28, 2008 2:39 PM, Keith C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thomas W. wrote:
> 
> > Not sure if that’s what you meant.
> 
> No, that’s not quite it.

I think what Thomas was meaning to point out is that you probably want  
to use Yaml’s block syntax:

[http://yaml4r.sourceforge.net/cookbook/#single\_ending\_newline](http://yaml4r.sourceforge.net/cookbook/#single_ending_newline)

> For example, my yml file-
> 
> source\_id:  
> a=2  
> b=2  
> if (a==b)  
> c=3  
> end

yml.sub(/source\_id:/, “source\_id: |”)

Judson
