Require and OO

Discussion in 'Ruby' started by aidy, May 23, 2006.

  1. aidy

    aidy Guest

    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
     
    aidy, May 23, 2006
    #1
    1. Advertising

  2. 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.
     
    Timothy Goddard, May 23, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don
    Replies:
    2
    Views:
    431
    Ralph Henneberg
    Dec 10, 2003
  2. jens Jensen
    Replies:
    0
    Views:
    548
    jens Jensen
    May 2, 2006
  3. Replies:
    4
    Views:
    280
    Fuzzyman
    Dec 23, 2004
  4. cungar
    Replies:
    0
    Views:
    380
    cungar
    Nov 8, 2007
  5. Tobias Peters

    "require and subdirectories" problem

    Tobias Peters, Sep 9, 2003, in forum: Ruby
    Replies:
    2
    Views:
    76
    Idan Sofer
    Sep 9, 2003
Loading...

Share This Page