3 yaml questions...

M

Martin Pirker

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?


2) 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...?


3) yaml+bzip2

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


Thanks!
Martin
 
W

why the lucky stiff

Martin said:
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.
3) 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
 
M

Martin Pirker

why the lucky stiff said:
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
 
W

why the lucky stiff

Martin said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

What is YAML::Syck::Map? 1
YAML and NArray 1
Deep YAML 5
require 'yaml' fails 1
YAML + ASCII Encoded Unicode 1
yaml bug(?) in pre2 0
ChatBot 4
[ANN] Kwalify 0.6.0 - a schema validator for YAML and JSON 0

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top