Line breaks in YAML

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?

Shameless bumb. Stuck at work.

On Jan 28, 2008 10:59 PM, Keith C. [email protected] 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.

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

On Jan 28, 2008 2:39 PM, Keith C. [email protected] 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

For example, my yml file-

source_id:
a=2
b=2
if (a==b)
c=3
end

yml.sub(/source_id:/, “source_id: |”)

Judson

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs