# Problems with mechanize and inheritance

**URL:** <https://www.ruby-forum.com/t/problems-with-mechanize-and-inheritance/184507>\
**Category:** Ruby\
**Created:** [March 3, 2010, 5:53pm UTC](https://www.ruby-forum.com/t/problems-with-mechanize-and-inheritance/184507 "2010-03-03T17:53:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![thiagobrandam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@thiagobrandam](https://www.ruby-forum.com/u/thiagobrandam)\
**Post date:** [March 3, 2010, 5:53pm UTC](https://www.ruby-forum.com/t/problems-with-mechanize-and-inheritance/184507/1 "2010-03-03T17:53:58Z")

</div>

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](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](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.

---

<div class="post-metadata">

**Author:** ![thiagobrandam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c4cdca/32.png) [@thiagobrandam](https://www.ruby-forum.com/u/thiagobrandam)\
**Post date:** [March 3, 2010, 9:48pm UTC](https://www.ruby-forum.com/t/problems-with-mechanize-and-inheritance/184507/2 "2010-03-03T21:48:08Z")

</div>

On Mar 3, 1:53 pm, thiagobrandam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://www.google.com)’
