When you use a web browser, your browser is said to be the "user agent." In other words, it is a program that acts on your behalf. The Mechanize object is your Ruby program's "agent," and it's the first class you'll use when starting any Mechanize program.
The Mechanize agent object is the heart and soul of Mechanize. It abstracts any and all interaction with web servers, HTTP, cookies, HTTP authentication, connecting to proxies, etc. And while you won't be interacting with it all that much after you request your first page, it's always there in the background working for you.
Table of Contents
- Creating a Mechanize Object
- Getting Your First Page
- Getting a Page via a POST Request
- Using an HTTP Proxy
- Logging What Mechanize Does
Creating a Mechanize Object
The Mechanize#new method takes no arguments, so creating a mechanize object is rather straightforward. All configuration needed will be done after the object is created by modifying its various attributes or calling methods.
agent = Mechanize.new
However, one thing that can be said is that Mechanize#new can accept a block and will yield itself. This small piece of syntax candy helps segment Mechanize configuration from program logic. Though functionally, it does nothing. The Mechanize#new method still returns the Mechanize object, not the result of the block.
agent = Mechanize.new do|a|
a.auth('admin', '12345')
a.user_agent_alias = "Windows IE 6"
end

