Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Require and OO

Reply
Thread Tools

Require and OO

 
 
aidy
Guest
Posts: n/a
 
      05-23-2006
Hi,

[wasn't sure which group to submit this post to Watir or Ruby, but even
though I use Watir, I think it is more of a Ruby question]

If I use 'require' for example on two files within a master file:

require 'browser'
require 'logon'

And these two files contain this code


browser.rb

require 'watir'

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


logon.rb

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

Will the i.e. instance be the same in logon as browser, or are they
different objects?
Should I be using this pre-fix '@@'?

I am uncomfortable that Ruby allows me to slide into functional
programming while I am consciously trying to move to OO.Is this a
problem do you think, for Web scripting?

Aidy

 
Reply With Quote
 
 
 
 
Timothy Goddard
Guest
Posts: n/a
 
      05-23-2006
Ruby code is never truly functional. All code written at the base level
is executed in the context of a singleton instance of Object called,
imaginatively, main. This should work, although it isn't a good idea.
This is really no different from using a global variable. Try wrapping
the whole thing up in a class.

e.g.

require 'watir'

class Browser
def initialize(url)
@ie = Watir::IE.new
@ie.goto(url)
@ie.maximize()
end
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

b = Browser.new

This is a much cleaner way of solving the problem. Ruby is fairly
lenient and doesn't force you to use clean concepts all the time like
languages such as Java do (this has upsides and downsides), but you
will get the most out of it if you use it properly.

 
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 require username and password to users requesting an .ashx page? jens Jensen ASP .Net 0 05-02-2006 12:01 PM
Firefox and RSS feeds that require authentication ttfg Firefox 0 03-25-2005 11:51 PM
What is PRISM.exe and these application require runtime to terminate in unusual ways errors I just started receiving? Rod Flash Computer Support 0 08-17-2004 04:20 AM
Problem with "require" and multiple modules. Don Perl 2 12-10-2003 10:15 AM
Require SPE 310s and MRP 300s - Anyone know a source? Pat Metheny Cisco 1 11-19-2003 10:18 PM



Advertisments