【问题标题】:passing arguments, ruby script [closed]传递参数,ruby 脚本
【发布时间】:2013-02-27 16:28:47
【问题描述】:

我正在尝试将 2 个空数组传递给一个方法,但我得到一个参数异常...我阅读了一圈但仍然无法弄清楚为什么会发生这种情况...

代码:

file = File.read("place_code.google.com")
paths = []
urls = []

def parseLog(file, paths)
    file.each_line do |line|
        match = line.match(/([a-z0-9]*GET\s)(.*puzzle\S*)/)
        if match 
            paths << match[2] 
        end
    end
    return paths
end

错误:

:in `parseLog': wrong number of arguments (0 for 2) (ArgumentError)

【问题讨论】:

  • 介意告诉我们你是如何调用这个方法的吗?
  • Hmmmmm...您说得对,感谢您指出...我在一些更改后忘记更改调用的参数。谢谢你。让它成为一个回答者,所以我可以接受这个好的提示作为回答者。我忘了参数化调用

标签: ruby parameter-passing


【解决方案1】:

当你调用你的方法时,你并没有传入所需的参数。要调用您的方法,您可以执行以下操作:

file = File.read("place_code.google.com")
paths = []
urls = []
class NewClass
     def initalize
     end
     def parseLog(file, paths)
        file.each_line do |line|
          match = line.match(/([a-z0-9]*GET\s)(.*puzzle\S*)/)
          if match 
            paths << match[2] 
          end
       end
      return paths
     end
  end
yourClass = NewClass.new()
yourClass.parseLog(file, path)

【讨论】:

    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    相关资源
    最近更新 更多