【问题标题】:unable to access a method variable which is out of same file无法访问不在同一文件中的方法变量
【发布时间】:2012-03-22 14:54:41
【问题描述】:

当我将一段代码移动到另一个文件时,我无法使用方法。

下面提到的代码有效,因为所有代码都在一个文件中。

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

在上面的代码中,“URL”出现在名为 testdata.xls 的 Excel 工作表中。上面的代码工作得很好。假设这个文件名为 file1.rb

但是我想将浏览器的打开移动到不同的文件中,这样我就不会在所有测试文件中使用此代码,而是仅在一个文件中使用它并从中调用所有其他测试。以下是我所做的更改,但这不起作用。

在 File1.rb 中我保留了

所有必需的文件+我提到的打开浏览器的常用代码的新文件

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

我已经创建了一个文件来打开我想用作常用功能的浏览器。让我们在function.rb中说这个文件名

所有必需的文件+下面的代码

include Watir

module Commonfunctions


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

end

现在,当我运行 file1.rb 时,出现以下错误

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>'

“记录”对象来自不同的文件,即我的数据驱动器。在该文件中,这是存在方法记录的代码

    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

谁能帮我解决这个问题。我想将所有常用函数移动到一个文件中,并在所有测试中使用这些函数,而不是在所有测试中编写相同的函数。

【问题讨论】:

    标签: ruby methods include watir


    【解决方案1】:

    record 是一个词法范围的变量,在 args 中定义到以 myData.each do |record| 开头的块。由于它是词法范围的,它不能在它声明的上下文之外被名称引用,所以你必须将它传递给你显式创建的方法。

    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
    

    顺便说一句,在您的问题中,您声明“'记录'对象来自不同的文件”。这不是真的,因为我已经指出在您的问题上下文中设置 record 的位置。您在这里感到困惑,因为您使用的通用名称很差,并且没有编写范围有限的小代码块。

    感谢您努力重构以改进代码组织。您的代码示例还有很多值得关注的地方:一些风格(camelCaseNames,随机缩进),一些结构性(无 OO,命令式文件脚本),一些句法陷阱(方法名称和 args 的第一个括号之间的空格),一些成语(可枚举方法的糟糕命令)。与经验丰富的 ruby​​ 程序员一起浏览您的代码并在代码审查中寻求指点,您可能会受益。

    【讨论】:

    • 感谢您的回复。我尝试了您提到的内容并按预期工作。此外,我还有一个问题。如果我的函数 openbrowser 记录中有 2 个参数,例如 @ie.goto (record['Url']) 和 (:name, "Q").set (record['test]),该怎么办?那怎么处理..
    • @anagraj 只需向def openbrowser 添加更多参数,并在其使用的代码和方法中酌情使用它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 2018-09-24
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多