Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > can call a method, but not a method within a user-defined class

Reply
Thread Tools

can call a method, but not a method within a user-defined class

 
 
aidy
Guest
Posts: n/a
 
      06-04-2006
I have two ruby files.

In the client file, I can do this:

require 'browser'
start_browser("http://gbahevm07l15:9081/wps/portal")

In the browser file I have this method

def start_browser (url)
......
@ie.goto(url)
......
end

However if I envelop this method within a user defined class

class Browser
def start_browser (url)
......
end
end

And in the client file, do this:

require 'browser'

Browser.new.start_browser("http://gbahevm07l15:9081/wps/portal")

I receive this error

'undefined method `start_browser' for main:Object (NoMethodError)'

and do not know why.

Could anyone help

Thanks

Aidy

 
Reply With Quote
 
 
 
 
Daniel Schierbeck
Guest
Posts: n/a
 
      06-04-2006
aidy wrote:
> class Browser
> def start_browser (url)
> ......
> end
> end
>
> And in the client file, do this:
>
> require 'browser'
>
> Browser.new.start_browser("http://gbahevm07l15:9081/wps/portal")
>
> I receive this error
>
> 'undefined method `start_browser' for main:Object (NoMethodError)'


Please post more code -- it's impossible to help you unless we have
something to work with.


Daniel
 
Reply With Quote
 
 
 
 
aidy
Guest
Posts: n/a
 
      06-04-2006
Hi,

I apologise, as I was not clear at all.

browser.rb

require 'watir'

class Browser
def start_browser (url)
@ie = Watir::IE.new
@ie.goto(url)
@ie.maximize()
end
end

logon.rb

class Login
def login (username, password)
@ie.link(:text, 'Log in').click
@ie.text_field(:name, 'userid').set(username)
@ie.text_field(:name, 'password').set(password)
@ie.button(:value,'Log in').click
end
end

test_1.rb

require 'browser'
require 'logon'

Browser.new.start_browser("http://gbahevm07l15:9081/wps/portal")
Login.new.login("aidy","12345")


the error I actually get is this

'login': undefined method `link' for nil:NilClass (NoMethodError)

Now, if I remove the classes: Browser + Login, but keep the methods.

And in test_1.rb, I do this


start_browser("http://gbahevm07l15:9081/wps/portal")
login("aidy","12345")

the test runs fine.

Thank You

Aidy

 
Reply With Quote
 
Marcin Mielżyński
Guest
Posts: n/a
 
      06-04-2006
aidy wrote:

> class Browser
> def start_browser (url)
> @ie = Watir::IE.new
> @ie.goto(url)
> @ie.maximize()
> end
> end
>
> logon.rb
>
> class Login
> def login (username, password)
> @ie.link(:text, 'Log in').click
> @ie.text_field(:name, 'userid').set(username)
> @ie.text_field(:name, 'password').set(password)
> @ie.button(:value,'Log in').click
> end
> end
>


>
> 'login': undefined method `link' for nil:NilClass (NoMethodError)
>
> Now, if I remove the classes: Browser + Login, but keep the methods.



The problem is that in:

def start_browser (url)
> @ie = Watir::IE.new


def login (username, password)
> @ie.link(:text, 'Log in').click


you are not referring the same instance variable (@ie in Login class is
not even initialized). They belong to separate classes and thus separate
instances.

Two solutions come to my mind right now:

put start_browser and login methods in the same class or
use class variables to share them between Browser and Login class instances.

lopex
 
Reply With Quote
 
aidy
Guest
Posts: n/a
 
      06-04-2006
Hi Marcin
> use class variables to share them between Browser and Login class instances.


Thanks for your patience, but is this a valid way of sharing class
variables?

browser.rb

require 'watir'
class Browser
def start_browser(url)
@@ie = Watir::IE.new
@@ie.goto(url)
@@ie.maximize()
end
end

logon.rb

require 'browser'
class Login < Browser
def login(username, password)
@@ie.link(:text, 'Log in').click
@@ie.text_field(:name, 'userid').set(username)
@@ie.text_field(:name, 'password').set(password)
@@ie.button(:value,'Log in').click
end
end


test_case.rb

require 'browser'
require 'logon'
Browser.new.start_browser("http://gbahevm07l15:9081/wps/portal")
Login.new.login("aidy","12345")

Cheers

Aidy

 
Reply With Quote
 
Marcin Mielżyński
Guest
Posts: n/a
 
      06-04-2006
aidy wrote:

>
> require 'watir'
> class Browser
> def start_browser(url)
> @@ie = Watir::IE.new
> @@ie.goto(url)
> @@ie.maximize()
> end
> end
>
> logon.rb
>
> require 'browser'
> class Login < Browser
> def login(username, password)
> @@ie.link(:text, 'Log in').click
> @@ie.text_field(:name, 'userid').set(username)
> @@ie.text_field(:name, 'password').set(password)
> @@ie.button(:value,'Log in').click
> end
> end


In this case it is not necessary since you've introduced inheritance
between Browser and Login classes. In this case Browser instance
variables are inherited by Login and thus they are the same variables. I
meant a case where these two classes were not related:

class A

def A.ie
@@ie
end

def meth1
@@ie = 'Foo'
end

end

class B
def meth2
A.ie # here @@ie A class class variable is referenced
end
end

so:

a=A.new
a.meth1
b=B.new
b.meth2

will work as you expected (although it is not a good programming pattern

As I mentioned before, the best way is to put start_browser and login
methods in one class since they share same context (@ie instance here).
Furthermore I suggest to call start_browser in initialize method to make
it run automatically on instance creation.

hope that helps

lopex

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call a method within another method Paul Roche Ruby 3 10-06-2010 04:02 AM
why can't an instance instantiated within a class method access aprotected instance method? Greg Hauptmann Ruby 9 06-16-2008 10:16 AM
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
how to call method of the class which contains a pointer to other class method? Pawel_Iks C++ 3 07-31-2007 06:30 AM
Help with a utility class and having a method call another method from within Ian Python 2 09-06-2003 03:35 PM



Advertisments