如何在Ruby中使用外部文本文件创建for循环

如何在Ruby中使用外部文本文件创建for循环,ruby,rubygems,Ruby,Rubygems,我正在尝试遍历包含多行的列表VulnsList.txt 当我运行下面的脚本失败时,现在如果我删除列表中除一行之外的所有行,代码将完成 我相信代码并不是一行一行地在列表中循环 File.open("VulnsList.txt") do |f| f.each_line do |vuln| rsn = "Other" reporter_comments = "This exception has been approved for 60 days

我正在尝试遍历包含多行的列表VulnsList.txt

当我运行下面的脚本失败时,现在如果我删除列表中除一行之外的所有行,代码将完成

我相信代码并不是一行一行地在列表中循环

File.open("VulnsList.txt") do |f|
     f.each_line do |vuln|
           rsn = "Other" 
           reporter_comments = "This exception has been approved for 60 days."
           assets = nsc.group_assets(ag)            
           assets.each do |asset|    
           exc = VulnException.new(vuln, VulnException::Scope::ALL_INSTANCES_ON_A_SPECIFIC_ASSET, rsn)    
           exc.asset_id = asset.id    
           exc.save(nsc, reporter_comments)
        end 
    end
end
这是文件的内容:

通用icmp时间戳

jenkins-2018-04-11_cve-2018-1000169

jenkins-2018-04-11_cve-2018-1000170

这是我收到的错误:

Retrying the request due to undefined method `sub' for nil:NilClass. If you see
this message please open an Issue on Github with the error.



Retrying the request due to undefined method `sub' for nil:NilClass. If you see
this message please open an Issue on Github with the error.



Retrying the request due to undefined method `sub' for nil:NilClass. If you see
this message please open an Issue on Github with the error.



Retrying the request due to undefined method `sub' for nil:NilClass. If you see
this message please open an Issue on Github with the error.



Retrying the request due to undefined method `sub' for nil:NilClass. If you see
this message please open an Issue on Github with the error.

Traceback (most recent call last):
        9: from Exceptions_by_Asset_Group - Copy.rb:48:in `<main>'
        8: from Exceptions_by_Asset_Group - Copy.rb:48:in `open'
        7: from Exceptions_by_Asset_Group - Copy.rb:49:in `block in <main>'
        6: from Exceptions_by_Asset_Group - Copy.rb:49:in `each_line'
        5: from Exceptions_by_Asset_Group - Copy.rb:54:in `block (2 levels) in <
main>'
        4: from Exceptions_by_Asset_Group - Copy.rb:54:in `each'
        3: from Exceptions_by_Asset_Group - Copy.rb:57:in `block (3 levels) in <
main>'
        2: from D:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/nexpose-7.2.1/lib/nexpose
/vuln_exception.rb:230:in `save'
        1: from D:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/nexpose-7.2.1/lib/nexpose
/connection.rb:124:in `execute'
D:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/nexpose-7.2.1/lib/nexpose/api_request.rb:
148:in `execute': NexposeAPI: Action failed: Nexpose service returned an unrecog
nized response: "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n
<Failure error-code=\"-1\">\n    <Message>Error encountered, unable to fulfill r
equest.</Message>\n    <Exception>\n        <Message></Message>\n        <Stackt
race></Stacktrace>\n    </Exception>\n</Failure>\n" (Nexpose::APIError)
jenkins-2018-05-09_cve-2018-1000192
由于nil:NilClass的未定义方法“sub”,正在重试请求。如果你看到 此消息请在Github上打开一个带有错误的问题。 由于nil:NilClass的未定义方法“sub”,正在重试请求。如果你看到 此消息请在Github上打开一个带有错误的问题。 由于nil:NilClass的未定义方法“sub”,正在重试请求。如果你看到 此消息请在Github上打开一个带有错误的问题。 由于nil:NilClass的未定义方法“sub”,正在重试请求。如果你看到 此消息请在Github上打开一个带有错误的问题。 由于nil:NilClass的未定义方法“sub”,正在重试请求。如果你看到 此消息请在Github上打开一个带有错误的问题。 回溯(最近一次呼叫最后一次): 9:从异常中按资产组复制。rb:48:in`' 8:由资产组复制的例外。rb:48:在“打开”中 7:从例外情况按资产组复制。rb:49:in'block in' 6:由资产组复制的例外情况。rb:49:“每行”中 5:从例外情况按资产组复制。rb:54:in`block(2级)in< 主要>' 4:由资产集团提供的例外情况-副本。rb:54:在“每个”中 3:从例外情况按资产组复制。rb:57:in`block(3级)in< 主要>' 2:来自D:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/nexpose-7.2.1/lib/nexpose /vuln_异常.rb:230:in'save' 1:from D:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/nexpose-7.2.1/lib/nexpose /connection.rb:124:in'execute' D:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/nexpose-7.2.1/lib/nexpose/api_request.rb: 148:在“执行”中:NexposeAPI:操作失败:Nexpose服务返回一个unrecog 无效的响应:“\n” \n遇到错误,无法完成r 请求。\n\n\n\n\n\n“(Nexpose::APIRROR) jenkins-2018-05-09_cve-2018-1000192
我想出来了。我在变量中添加了.chomp,它成功了

谢谢

exc = VulnException.new(vuln, VulnException::Scope::ALL_INSTANCES_ON_A_SPECIFIC_ASSET, rsn)

exc = VulnException.new(vuln.chomp, VulnException::Scope::ALL_INSTANCES_ON_A_SPECIFIC_ASSET, rsn)

它是模糊的。会发生什么?意外的结果?错误?错误消息是什么?该文件是什么样子的?请发布一个指向该文件的链接或该文件的示例数据。您在发布的代码片段中也缺少了另一个结尾,但我怀疑您只是在发布问题时遗漏了它,而不是在代码中。您向我们显示的代码缺失,并且
end
语句,您可以更正它吗?@Martinos如果代码缺失
end
语句,然后问题需要关闭,因为它有语法错误。end语句在原始代码中。我遇到的问题是让这个脚本每行迭代一次。当我测试脚本时,列表中的所有行立即打印出来。