Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 处理文件路径参数:没有将nil隐式转换为字符串_Ruby - Fatal编程技术网

Ruby 处理文件路径参数:没有将nil隐式转换为字符串

Ruby 处理文件路径参数:没有将nil隐式转换为字符串,ruby,Ruby,我正在编写一个简短的ruby脚本,它将一个文件作为参数,然后解析该文件。 我在initialize方法中设置了一些条件,以确保文件路径存在且可读,如果不存在,则向用户打印错误消息 但是,当我运行文件时,没有在消息“请添加日志文件路径”旁边附加文件。我还收到以下错误消息 please add log file path Traceback (most recent call last): 3: from webserver_log_parser.rb:41:in `<main>

我正在编写一个简短的ruby脚本,它将一个文件作为参数,然后解析该文件。 我在initialize方法中设置了一些条件,以确保文件路径存在且可读,如果不存在,则向用户打印错误消息

但是,当我运行文件时,没有在消息“请添加日志文件路径”旁边附加文件。我还收到以下错误消息

please add log file path
Traceback (most recent call last):
    3: from webserver_log_parser.rb:41:in `<main>'
    2: from webserver_log_parser.rb:41:in `new'
    1: from webserver_log_parser.rb:6:in `initialize'
webserver_log_parser.rb:6:in `exist?': no implicit conversion of nil into String (TypeError)
请添加日志文件路径
回溯(最近一次呼叫最后一次):
3:来自webserver\u log\u解析器。rb:41:in`'
2:来自webserver_log_parser.rb:41:在'new'中
1:来自Web服务器\u日志\u解析器。rb:6:在'initialize'中
webserver_log_parser.rb:6:在'exist'中:没有将nil隐式转换为字符串(TypeError)
如果有人能解释为什么会发生这种情况以及解决这个问题的方法,我将不胜感激

  def initialize(log_file_path = nil)
    puts 'please add log file path' unless log_file_path
    puts 'Could not find the file path' unless File.exist?(log_file_path)
    puts '${log_file_path} is unreadable' unless File.readable?(log_file_path)
    extract_log_file(log_file_path)
  end

  def extract_log_file(log_file_path)
    webpages = Hash.new { |url, ip_address| url[ip_address] = [] }
    File.readlines(log_file_path).each do |line|
      url, ip_address = line.split
      webpages[url] << ip_address
    end
    sort_results(webpages)
  end

  def sort_results(results)
    total_views = {}
    unique_views = {}
    results.each do |url, ip_address|
      total_views[url] = ip_address.length
      unique_views[url] = ip_address.uniq.length
    end
    display_results(total_views, unique_views)
  end

  def display_results(views, unique_views)
    puts 'The most viewed pages are as follows:'
    total_views_sorted = views.sort_by { |_k, count| -count }
    total_views_sorted.each { |key, count| puts "#{key} #{count}" }
    puts 'The pages with the most unique views are as follows:'
    unique_sort = unique_views.sort_by { |_k, count| -count }
    unique_sort.each { |key, count| puts "#{key} #{count}" }
  end
end

if $PROGRAM_NAME == __FILE__
  LogParser.new(ARGV[0])
end```

def初始化(日志文件路径=nil)
放置“请添加日志文件路径”,除非日志文件路径
puts“无法找到文件路径”,除非file.exist?(日志文件路径)
放置“${log\u file\u path}不可读”,除非file.readable?(log\u file\u path)
提取日志文件(日志文件路径)
结束
def提取日志文件(日志文件路径)
webpages=Hash.new{| url,ip地址| url[ip地址]=[]
读取行(日志文件路径)。每个do行|
url,ip_地址=line.split

网页[url]当您的保护条件被触发时,您需要停止进一步的处理(如果您已经确定
文件路径
为零,则无需检查
文件路径
处的文件可读性)。它可能看起来像这样,例如:

def initialize(log_file_path = nil)
  unless log_file_path
    puts 'please add log file path' 
    return
  end
  unless File.exist?(log_file_path)
    puts 'Could not find the file path' 
    return
  end
  unless File.readable?(log_file_path)
    puts '${log_file_path} is unreadable' 
    return
  end

  extract_log_file(log_file_path)
end

如果要使用消息直接终止脚本,可以使用
abort

def initialize(log_file_path = nil) 
    abort("please add log file path") unless log_file_path
    abort("Could not find the file path") unless File.exist?(log_file_path)
    abort("${log_file_path} is unreadable") unless File.readable?(log_file_path)
    extract_log_file(log_file_path)
end

您的错误消息包含行号41和6。哪些行对应于您发布的代码中的行?