Loading one YAML object at a time

I’d like to write a series of YAMLized objects to a file, then read
them in one by one.

I tried this test:
rick@frodo:/public/rubyscripts$ cat multiyaml.rb
require ‘yaml’

File.open(‘testyaml’,‘w’) do |f|
YAML.dump(“string1”, f)
YAML.dump(“string2”, f)
end

File.open(“testyaml”) do |f|
p YAML.load(f)
p YAML.load(f)
end

File.open(“testyaml”) do |f|
YAML.each_document(f) {|obj| p obj}
end

rick@frodo:/public/rubyscripts$ ruby multiyaml.rb
“string1”
false
“string1”
“string2”

It looks like YAML.load isn’t properly leaving the file positioned to
read the second object. Is this a bug?

YAML.each_document seems to do the right thing, but I’m concerned
about the memory overhead for large files.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/