# Loading one YAML object at a time

**URL:** <https://www.ruby-forum.com/t/loading-one-yaml-object-at-a-time/87644>\
**Category:** Ruby\
**Created:** [March 4, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/loading-one-yaml-object-at-a-time/87644 "2007-03-04T20:41:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [March 4, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/loading-one-yaml-object-at-a-time/87644/1 "2007-03-04T20:41:38Z")

</div>

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/](http://talklikeaduck.denhaven2.com/)
