3 yaml questions

Hi… I need help :-/

“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

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

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 wrote:

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.