YAML::parse_documents( (String or IO) io, Proc doc_proc )
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'
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}"
}
The IO object is closed upon completion of parsing.