From: thiagobrandam on
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.



From: thiagobrandam on


On Mar 3, 1:53 pm, thiagobrandam <thiagobran...(a)gmail.com> wrote:
> 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.

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'