0

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.

Community
  • 1
  • 1
Thufir
  • 8,216
  • 28
  • 125
  • 273
  • Why do you `require 'rubygems'` twice and require Net::HTTP and Net::HTTPS? The first is redundant and the last two shouldn't be needed if you are using Mechanize. – the Tin Man Jan 24 '12 at 00:41
  • oh, copy/paste as to having rubygems twice. I'll fix that and remove the net stuff. – Thufir Jan 24 '12 at 00:45
  • fixed the require stuff, pardon. Anyhow, same result, the full class is still required on my system. Question still valid. thank you though. – Thufir Jan 24 '12 at 00:49

1 Answers1

1

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • well, outside the character limit to give full paste. However, same result in irb as in ruby for me, though. Admittedly I'm in the process of upgrading my system, so just use rubygems to bring in Mechanize. – Thufir Jan 24 '12 at 00:59
  • oh, I'm on Ubuntu but maybe it's a path problem as you suggest. Maybe that explains the previous question as well. – Thufir Jan 24 '12 at 01:09
  • !!!!! DEPRECATION NOTICE !!!!! The WWW constant is deprecated, please switch to the new top-level Mechanize constant. WWW will be removed in Mechanize version 2.0 You've referenced the WWW constant from ./bar.rb:14, please switch the "WWW" to "Mechanize". Thanks! Sincerely, Pew Pew Pew – Thufir Jan 24 '12 at 07:10