YAML::parse_documents Method
parse_documents
Iterates through documents in a stream, returning YamlNodes for each

YAML::parse_documents(
  (String or IO) io,
  Proc doc_proc
)

Parameters
io
The string or IO object to read from.
doc_proc
A procedure for handling each parsed document
Block Parameters
obj
An object containing the current document as a YamlNode
Return Values
Nil

Details

Just as YAML::parse provides access to the generic data of a document, YAML::parse_documents iterates through documents in a stream, providing a tree of YamlNodes for you to work on. 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::parse_documents, you can process each entry in a file 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. From there, YPath expressions or transformations could be applied:

require 'yaml'
log = File.open( "/var/log/apache.yaml" )
yp = YAML::parse_documents( log ) { |tree|
  at = tree.select('/at')[0].value
  type = tree.select('/type')[0].value
  puts "#{at} #{type}"
}
Ex. 44: Parsing YAML documents from a stream

The IO object is closed upon completion of parsing.