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:
[email protected]:/public/rubyscripts$ cat multiyaml.rb
require ‘yaml’

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

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

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

[email protected]:/public/rubyscripts$ ruby multiyaml.rb

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs