Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7696065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:41:36+00:00 2026-05-31T21:41:36+00:00

I am unable to use a method when I move a piece of code

  • 0

I am unable to use a method when I move a piece of code to another file.

Below mentioned code works cause all the code is in one file.

require 'rubygems'
require 'watir'
require 'win32ole'
require 'erb' 
require 'ostruct' 
require 'C:/classes/html.class'
require 'C:/classes/Xls'
require 'C:/classes/screen_capture'
require 'C:/classes/RequiredRubies'
include Watir
begin

      xlFile = XLS.new(Dir.pwd + '/testdata.xls') 
      myData = xlFile.getRowRecords('a2:z3','Pit') 
      xlFile.close
      myData.each do |record| 

      @ie = IE.new 
      @ie.maximize
      @ie.goto (record['Url'])
      @ie.focus

end
end

In above code ‘URL’ is present in an excel sheet names testdata.xls. The above code works just fine. Lets say this file name is file1.rb

But I want to move the opening of browser into different file so that I don’t use this code in all the test files and use it in only one file and call from it to all other test. Below is the change which I have done but this does not work.

In File1.rb I have kept

All Required Files+new file where I have mentioned the common code to open the browser

require 'C:/function.rb

include Watir
include Commonfunctions

begin

      xlFile = XLS.new(Dir.pwd + '/testdata.xls') 
      myData = xlFile.getRowRecords('a2:z3','Pit') 
      xlFile.close
      myData.each do |record| 

   openie = openbrowser

end

end

And I have created a file for opening the browser which I want to use as common function. Lets say this file name in function.rb

All Required files+below code

include Watir

module Commonfunctions


def openbrowser
      @ie = IE.new 
      @ie.maximize
      @ie.goto (record['Url'])
      @ie.focus
end

end

Now when I run my file1.rb I get the below error

C:/function.rb:17:in `openbrowser': undefined local variable or method `reco
rd' for main:Object (NameError)
        from test.rb:23:in `block in <main>'
        from test.rb:21:in `each'
        from test.rb:21:in `<main>'

The ‘record’ object is coming from different file which is my data drive. In that file this is the code where method record is present

    numRecords = myRange.Rows.Count
    (0..numRecords-1).each do |i|
      record=[]
      areas.each do |area|
        record.concat(area[i])
      end
      #Clean up formatting
      record.collect! do |x|
        if x.is_a?(Float) and x % 1 == 0
           x.to_i.to_s
         else
          x.to_s.strip
        end
      end
      data << record
    end
    return data
  end

Can any one please help me to resolve this issue. I want to move all the common functions to one file and use those functions in all test instead of writing the same functions in all tests.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:41:37+00:00Added an answer on May 31, 2026 at 9:41 pm

    record is a lexically scoped variable defined in the args to your block which begins myData.each do |record|. Since it’s lexically scoped it can not be referenced by name outside of the context it’s declared in, so you must pass it to the method you made explicitly.

    myData.each do |record| 
       openie = openbrowser record # pass it here
    end
    
    # and in the other file
    
    def openbrowser record
          @ie = IE.new 
          @ie.maximize
          @ie.goto (record['Url'])
          @ie.focus
    end
    

    BTW, In your question you state that “The ‘record’ object is coming from diffent file”. This is not true, as I’ve already pointed out where the record in the context of your issue is set. You were confused here because you’re using poor generic names and not writing small blocks of code with limited scope.

    I appreciate that you’re working to refactor to improve the code organization. There are many more things with your code samples that deserve some attention: some stylistic (camelCaseNames, random indentation), some structural (no OO, imperative file scripting), some syntactic traps (space between method name and first paren of args), some idiom (poor command of enumerable methods). You might benefit by walking through your code with an experienced ruby programmer and ask for pointers in a code review.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am unable to use the Include(CITies) extension method on the db.Restaurants object. When
I unable to use put method to send data to particular link.. Please find
I'm unable to use date (under OS X 10.6, using bash) without getting the
Typically languages have keywords that you are unable to use directly with the exact
Since I upgraded my iPhone to 5.1 I have been unable to use it
Being fairly new to JavaScript, I'm unable to discern when to use each of
I am working with the Shopsense Ruby Gem however I am unable to use
I am unable to get this code to sort a list of objects using
Example: I use a PDO method that throws an exception. But inside the try
Thanks to the answers I have figured out that I am unable to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.