thiagobrandam
thiagobrandam

Reputation: 91

Problem with Ruby mechanize and inheritance

I'm working with mechanize and having problems with inheritance when testing in a rails environment, using script/console.

When I write like this:

require 'rubygems'
require 'mechanize'

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

Everything works fine. But when I try to create a subclass of WWW::Mechanize like this:

require 'rubygems'
require 'mechanize'

class Alfa < WWW::Mechanize; end

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

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'

Am I missing something?

Thanks in advance

Upvotes: 0

Views: 513

Answers (1)

i-blis
i-blis

Reputation: 3179

When you subclass WWW::Mechanize, no HTML parser is provided : that is what the error line actually tells you.

This works :

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

Upvotes: 2

Related Questions