problems with mechanize and inheritance

T

thiagobrandam

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.
 
T

thiagobrandam

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 =3D 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 =3D 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.

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top