# 3 yaml questions

**URL:** <https://rubytalk.org/t/3-yaml-questions/11744>\
**Category:** ruby-talk\
**Created:** [26 May 2004 01:38 UTC](https://rubytalk.org/t/3-yaml-questions/11744 "2004-05-26T01:38:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Pirker](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@Martin\_Pirker](https://rubytalk.org/u/Martin_Pirker)\
**Post date:** [26 May 2004 01:38 UTC](https://rubytalk.org/t/3-yaml-questions/11744/1 "2004-05-26T01:38:44Z")

</div>

Hi… I need help :-/

1. 

“Array” of “String” objects  
each String is a news article  
dumping Array .to\_yaml gives:

— !seq

- "Path: …!not-for-mail\nFrom: …
- "Path: …!not-for-mail\nFrom: …
- "Path: …!not-for-mail\nFrom: …
- |  
Path: …!not-for-mail  
From: …  
Subject: …  
[…]
- "Path: …!not-for-mail\nFrom: …
- "Path: …!not-for-mail\nFrom: …  
[…]

Question: Why are some articles one liners with \n while others are  
dumped in original linebreak style?

1. in yaml4r cookbook:

> Simple Inline Array  
> Sequences can be contained on a single line, using the inline syntax.

So arrays don’t dump in  
seq:

- a
- b
- c

but in  
seq: [a, b, c]

Question: So far I was unable to dump the short style from Ruby - how  
to…?

1. yaml+bzip2

The yaml ascii dumps may get large, any easy way to auto filter  
load/save through bzip2?

Thanks!  
Martin

---

<div class="post-metadata">

**Author:** ![why\_the\_lucky\_stiff1](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@why\_the\_lucky\_stiff1](https://rubytalk.org/u/why_the_lucky_stiff1)\
**Post date:** [26 May 2004 07:15 UTC](https://rubytalk.org/t/3-yaml-questions/11744/2 "2004-05-26T07:15:44Z")

</div>

Martin Pirker wrote:

> Hi… I need help :-/

Hello, Martin and his slanty jaw.

> — !seq
> 
> - "Path: …!not-for-mail\nFrom: …
> - "Path: …!not-for-mail\nFrom: …
> - "Path: …!not-for-mail\nFrom: …
> - |  
> Path: …!not-for-mail  
> From: …  
> Subject: …  
> […]
> - "Path: …!not-for-mail\nFrom: …
> - "Path: …!not-for-mail\nFrom: …  
> […]
> 
> Question: Why are some articles one liners with \n while others are  
> dumped in original linebreak style?

The String#to\_yaml method uses a routine to find a good fit for whatever  
string you’re passing in. It might be because there are some  
non-printables or some whitespace that needs to be preserved.

In the last few weeks I’ve recoded alot of that logic. I think it is  
becoming superior. (Try Ruby CVS.)

If you want to force certain objects to be block literals (indicated by  
the pipe character), you can create `to\_yaml\_fold’ methods for those  
objects. Either do it in the class definition or in the singleton.

o = “Here’s one in a singleton”  
def o.to\_yaml\_fold; ‘|’; end  
puts o.to\_yaml

outputs:  
— |  
Here’s one in a singleton

This sort of technique is only available in the latest snapshots. But  
it will be prevalent by Ruby 1.8.2.

> seq: [a, b, c]
> 
> Question: So far I was unable to dump the short style from Ruby - how  
> to…?

The emitter isn’t equipped to dump inline arrays currently. Shortly it  
will be available similiar to what you see above.

o = [‘a’, ‘b’, ‘c’]  
def o.to\_yaml\_format; :inline; end

I haven’t decided completely on this, though. So, we’ll see.

> 1. yaml+bzip2
> 
> The yaml ascii dumps may get large, any easy way to auto filter  
> load/save through bzip2?

Sure. All the YAML loading/parsing methods accept IO objects. If the  
file has a bz2 suffix, use the BZ2::Reader.

reader = if filename =~ /.bz2$/  
BZ2::Reader  
else  
File  
end  
obj = reader.open( filename ) do |f|  
YAML::load( f )  
end

YAML::dump can write to IO.

writer = if filename =~ /.bz2$/  
BZ2::Writer  
else  
File  
end  
writer.open( filename, ‘w’ ) do |f|  
YAML::dump( obj, f )  
end

\_why

---

<div class="post-metadata">

**Author:** ![Martin\_Pirker](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@Martin\_Pirker](https://rubytalk.org/u/Martin_Pirker)\
**Post date:** [28 May 2004 12:59 UTC](https://rubytalk.org/t/3-yaml-questions/11744/3 "2004-05-28T12:59:34Z")

</div>

> Martin Pirker wrote:

> The String#to\_yaml method uses a routine to find a good fit for whatever  
> string you’re passing in. It might be because there are some  
> non-printables or some whitespace that needs to be preserved.
> 
> In the last few weeks I’ve recoded alot of that logic. I think it is  
> becoming superior. (Try Ruby CVS.)
> 
> If you want to force certain objects to be block literals (indicated by  
> the pipe character), you can create `to\_yaml\_fold’ methods for those  
> objects. Either do it in the class definition or in the singleton.

I’d prefer the “one line” style for archival and easy interfacing  
to others, but the “normal” style for debugging.  
So choice with parameter.

Some articles dumped with double line spacing (ugly…), but setting  
“Bestwidth=\>200” helped.

I searched for line-breaks, indentations or other special chars but couldn’t  
figure out for now what’s triggering which style.

I’m running “ruby 1.8.1 (2004-04-24) [i686-linux.gnu]”

> The emitter isn’t equipped to dump inline arrays currently. Shortly it  
> will be available similiar to what you see above.

ok

> Sure. All the YAML loading/parsing methods accept IO objects. If the  
> file has a bz2 suffix, use the BZ2::Reader.  
> […]

Thanks!

Martin

> **···**
>
> why the lucky stiff [ruby-talk@whytheluckystiff.net](mailto:ruby-talk@whytheluckystiff.net) wrote:

---

<div class="post-metadata">

**Author:** ![why\_the\_lucky\_stiff1](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@why\_the\_lucky\_stiff1](https://rubytalk.org/u/why_the_lucky_stiff1)\
**Post date:** [28 May 2004 13:00 UTC](https://rubytalk.org/t/3-yaml-questions/11744/4 "2004-05-28T13:00:56Z")

</div>

Martin Pirker wrote:

> Some articles dumped with double line spacing (ugly…), but setting  
> “Bestwidth=\>200” helped.

The ‘double line spacing’ happens with folded blocks. In 1.8.1, it is  
possible to use literal blocks (which doesn’t wrap) with :UseBlock =\> true.
