From: why the lucky stiff Date: 2004-05-26T16:15:44+09:00 Subject: Re: 3 yaml questions... 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. > > 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