Question

why do you need the full name for mechanize as so:

#!/usr/bin/ruby -w

require 'rubygems'
require 'pp'
require 'yaml'
require "mechanize"


yml = YAML.load_file 'login.yml'
user = yml["user"]
pword = yml["pword"]

a = WWW::Mechanize.new { |agent|
  agent.user_agent_alias = 'Mac Safari'
}

a.get('http://google.com/') do |page|
  search_result = page.form_with(:name => 'f') do |search|
    search.q = 'Hello world'
  end.submit

  search_result.links.each do |link|
    puts link.text
  end
end

when the mechanize example doesn't do that? This is asked on top of a previous question. Code only worked after reading the previous question on this exact topic and adding the full class(?) name. I've seem somewhat similar in Java, but only when it's ambiguous. Here, there's nothing ambigious, there's only the one Mechanize.

Pardon, the actual previous question completely contradicts the above link. The previous question I was referencing is here. To reiterate, two different questions, two different answers. Maybe the API or idiom changed.

Was it helpful?

Solution

What version of Mechanize are you using? Try gem list mechanize.

Using Ruby 1.8.7-p357, 1.9.2-p290, and 1.9.3-p0 and Mechanize 2.1 I am able to instantiate an instance. For instance:

1.8.7 :001 > require 'mechanize'
true
1.8.7 :002 > agent = Mechanize.new
#<Mechanize:0x101baacf0
[...]

and:

1.9.3p0 :001 > require 'mechanize'
true
1.9.3p0 :002 > agent = Mechanize.new
#<Mechanize:0x102988610
[...]

I suspect you are using Mac OS, because you are accessing Ruby at /usr/bin. Ruby is not installed by default on Windows or Linux and wouldn't be at that path normally.

Apple's version of Ruby doesn't include Mechanize, so you added it at some point. Because Apple didn't install it it should be benign to update, so do:

sudo gem update mechanize

Apple does use Ruby for apps on Mac OS, so you have to be aware of that when updating their pre-installed gems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top