Using WWW:Mechanize to download a file to disk without loading it all in memory first

What you really want is the Mechanize::Download

http://mechanize.rubyforge.org/Mechanize/Download.html

you can use this way:

require 'mechanize'

agent = Mechanize.new
agent.pluggable_parser.default = Mechanize::Download
agent.get('http://example.com/foo').save('a_file_name')

Have you looked at Mechanize::FileSaver? It looks like it can do what you require.

Here is an example that saves all the PDF files it encounters:

require 'rubygems'
require 'mechanize'

agent = Mechanize.new
agent.pluggable_parser.pdf = Mechanize::FileSaver
agent.get('http://example.com/foo.pdf')