# Watir and Inheritance

**URL:** <https://www.ruby-forum.com/t/watir-and-inheritance/118085>\
**Category:** Ruby\
**Created:** [October 23, 2007, 7:22pm UTC](https://www.ruby-forum.com/t/watir-and-inheritance/118085 "2007-10-23T19:22:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [October 23, 2007, 7:22pm UTC](https://www.ruby-forum.com/t/watir-and-inheritance/118085/1 "2007-10-23T19:22:25Z")

</div>

## I’m trying to do this:

require ‘Watir’

class Navigator\<Watir::IE  
def initialize(maxim)  
@maxim=maxim  
@iex=Watir::IE.new  
end  
def go(url)  
@iex.goto(url)  
if @maxim==1 then  
@iex.maximize()  
end  
end  
end

naveg=Navigator.new(1)  
naveg.go(“[http://www.google.es](http://www.google.es)”)

## puts naveg.url

I want my ‘naveg’ object can display the methods and properties of  
wakir, url for example. What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [October 23, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/watir-and-inheritance/118085/2 "2007-10-23T19:30:48Z")

</div>

Without looking at this too indepth…  
you are inheriting from Watir::IE, and then making a new instance of  
it inside of it? Ehh?

Start off with changing  
class Navigator\<Watir::IE  
to  
class Navigator

Now your class will work (but won’t be inheriting). I don’t know what  
you really want to do with your class 🙂 but hey, have fun playing  
with it!

–Kyle

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [October 23, 2007, 7:36pm UTC](https://www.ruby-forum.com/t/watir-and-inheritance/118085/3 "2007-10-23T19:36:22Z")

</div>

Mario R. wrote:

> I want my ‘naveg’ object can display the methods and properties of  
> wakir, url for example. What am I doing wrong?

You want to call the url method on the @iex object, not the navigator  
object. Try this:

require ‘Watir’

class Navigator\<Watir::IE  
def method\_misssing(name,\*args)  
if @iex.respond\_to? name  
@iex.send(name,\*args)  
end  
end  
def initialize(maxim)  
@maxim=maxim  
@iex=Watir::IE.new  
end  
def go(url)  
@iex.goto(url)  
if @maxim==1 then  
@iex.maximize()  
end  
end  
end

naveg=Navigator.new(1)  
naveg.go(“[http://www.google.es](http://www.google.es)”)

puts naveg.url

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [October 24, 2007, 11:29am UTC](https://www.ruby-forum.com/t/watir-and-inheritance/118085/4 "2007-10-24T11:29:46Z")

</div>

If I write the class without inheriting, how can I get the ‘url’ outside  
of the class?

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [October 24, 2007, 11:31am UTC](https://www.ruby-forum.com/t/watir-and-inheritance/118085/5 "2007-10-24T11:31:17Z")

</div>

Drew O. wrote:

> You want to call the url method on the @iex object, not the navigator  
> object. Try this:  
> …

This is not working.

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [October 24, 2007, 11:42am UTC](https://www.ruby-forum.com/t/watir-and-inheritance/118085/6 "2007-10-24T11:42:23Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:…

I’m sorry but it doesn’t work because the class doesn’t understand the  
IE methods without the suffix.

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [October 24, 2007, 11:38am UTC](https://www.ruby-forum.com/t/watir-and-inheritance/118085/7 "2007-10-24T11:38:21Z")

</div>

On 10/23/07, Mario R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @iex.goto(url)
> 
> ```
> 
> * * *
> 
> I want my ‘naveg’ object can display the methods and properties of  
> wakir, url for example. What am I doing wrong?

I think what you are doing wrong is having a Watir::IE object inside  
your Navigator class. If you inherit from IE you have all methods in  
IE available so you don’t need to wrap another instance of IE inside.  
Can you try this (not tested):

require ‘Watir’

class Navigator\<Watir::IE  
def initialize(maxim)  
@maxim=maxim  
end  
def go(url)  
goto(url)  
if @maxim==1 then  
maximize()  
end  
end  
end

naveg=Navigator.new(1)  
naveg.go(“[http://www.google.es](http://www.google.es)”)

puts naveg.url

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [October 24, 2007, 12:15pm UTC](https://www.ruby-forum.com/t/watir-and-inheritance/118085/8 "2007-10-24T12:15:05Z")

</div>

By the way the error is:  
c:/ruby/lib/ruby/site\_ruby/1.8/Watir.rb:1341:in `url': undefined method`LocationURL’ for nil:NilClass (NoMethodError)  
from tempexample.rb:22

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [October 24, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/watir-and-inheritance/118085/9 "2007-10-24T14:46:10Z")

</div>

Watir’s ie object allows you to get at things like the url using  
public methods. Unless the idea really is extending or or replacing  
them, inheritance shouldn’t be necessary (or probably advised). Well,  
that is unless the whole point is to play around with Watir to learn  
about it, more than get work done 😉

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [October 24, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/watir-and-inheritance/118085/10 "2007-10-24T15:26:35Z")

</div>

I think Jesus’ approach is correct. It only missed the initialization of  
the  
super class:

class Navigator\<Watir::IE  
def initialize(maxim)  
super()  
@maxim=maxim  
end  
def go(url)  
goto(url)  
if @maxim==1 then  
maximize()  
end  
end  
end

At first I thought calling ‘super’ would do but then I was pointed out  
by  
him that ‘super’ calls the superclass’ initialize method with the same  
number of parameters, so we need parenthesis (good to know 🙂

Anyway with ‘super()’ it seems it works:

naveg=Navigator.new(1)  
naveg.go(“[http://www.google.es](http://www.google.es)”)

puts naveg.url # =\> =\> “[http://www.google.es/](http://www.google.es/)”

/RubÃ©n

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [October 24, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/watir-and-inheritance/118085/11 "2007-10-24T16:42:26Z")

</div>

Now it works fine.

Thank you very much.
