YAML::load_documents Method
Iterates through documents in a stream

  (String or IO) io,
  Proc doc_proc

The string or IO object to read from.
A procedure for handling each parsed document
Block Parameters
An object containing the current document
Return Values
A Ruby object


The YAML::load_documents method is great for parsing streaming data, especially data which has a fixed formatting. For example, let's suppose you are reading from a log file:

at: 2001-08-12 09:25:00.00 Z
type: GET
HTTP: '1.0'
url: '/index.html'
at: 2001-08-12 09:25:10.00 Z
type: GET
HTTP: '1.0'
url: '/toc.html'
Ex. 39: Stream containing a log file

Using YAML::load_documents, you can process each entry individually, without needing to allocate space for the entire file contents in memory. With each iteration, the current document is passed into the Proc you supply:

require 'yaml'
log = File.open( "/var/log/apache.yaml" )
yp = YAML::load_documents( log ) { |doc|
  puts "#{doc['at']} #{doc['type']} #{doc['url']}"
Ex. 40: Loading the log file with YAML::load_documents

The IO object is closed upon completion of parsing.