传递参数,ruby脚本

时间:2013-02-27 16:28:47

标签: ruby parameter-passing

我正在尝试将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)

1 个答案:

答案 0 :(得分: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)