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