[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Line breaks in YAML

Keith Carter

1/28/2008 9:16:00 PM

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?
--
Posted via http://www.ruby-....

4 Answers

Keith Carter

1/28/2008 9:59:00 PM

0

Shameless bumb. Stuck at work.
--
Posted via http://www.ruby-....

Thomas Wieczorek

1/28/2008 10:18:00 PM

0

On Jan 28, 2008 10:59 PM, Keith Carter <kmarplecarter@yahoo.com> 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.

Keith Carter

1/28/2008 10:39:00 PM

0

Thomas Wieczorek 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
>>

--
Posted via http://www.ruby-....

Judson Lester

1/28/2008 11:06:00 PM

0

On Jan 28, 2008 2:39 PM, Keith Carter <kmarplecarter@yahoo.com> wrote:
> Thomas Wieczorek 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_endi...

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

yml.sub(/source_id:/, "source_id: |")

Judson
--
Your subnet is currently 169.254.0.0/16. You are likely to be eaten by a grue.