Problems with mechanize and inheritance

Hi, I’m having problems with mechanize when I try to subclass it in a
rails environment using script/console.

The following code works fine:

require ‘rubygems’
require ‘mechanize’

agent = WWW::Mechanize.new
agent.get ‘http://www.google.com

But this one doesn’t work:

require ‘rubygems’
require ‘mechanize’

class Alfa < WWW::Mechanize; end

agent = Alfa.new
agent.get ‘http://www.google.com

Everytime I call the get instance method, I get the following error:

NoMethodError: You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.parse
from /Library/Ruby/Gems/1.8/gems/mechanize-0.9.3/lib/www/mechanize/
page.rb:77:in `parser’

I tried to look at the code in ‘page.rb’ but I haven’t been able to
figure out the error. Am I missing something here?

Thanks in advance.

On Mar 3, 1:53 pm, thiagobrandam [email protected] wrote:

Everytime I call the get instance method, I get the following error:

NoMethodError: You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.parse
from /Library/Ruby/Gems/1.8/gems/mechanize-0.9.3/lib/www/mechanize/
page.rb:77:in `parser’

I tried to look at the code in ‘page.rb’ but I haven’t been able to
figure out the error. Am I missing something here?

Thanks in advance.

In case someone gets the same error, you must explicitly set a html
parser :

class Agent < WWW::Mechanize
end
a = Agent.new
a.html_parser = Nokogiri::HTML
a.get ‘http://www.google.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs