【问题标题】:Ruby: unexpected ',', expecting keyword_endRuby:意外的',',期待keyword_end
【发布时间】:2017-05-22 21:30:41
【问题描述】:

对 Ruby 非常陌生,无法在这段代码中看到名义上的语法错误:

#! /usr/bin/env ruby                                                               

require 'sensu-plugin/metric/cli'                                                  

class MetricAvailableUpdates < Sensu::Plugin::Metric::CLI::Graphite                

  option :scheme,                                                                  
    description: 'Metric naming scheme',                                           
    long: '--scheme SCHEME',                                                       
    short: '-s SCHEME',                                                            
    default: "#{Socket.gethostname}"                                               

  def run                                                                          
    # Get the metrics.                                                             
    output = %x[/usr/lib/update-notifier/apt-check --human-readable]               
    output_lines = output.split(/(\n)/)                                            

    metrics = {}                                                                   

    updates_pattern = " packages can be updated."                                  
    updates = output_lines[0].tr(upgrades_pattern, "").to_i                        
    metrics[:available_updates] = updates                                          

    security_updates_pattern = " updates are security updates."                    
    security_updates = output_lines[2].tr(security_updates_pattern, "").to_i       
    metrics[:available_security_updates] = security_updates                        

    # Print them in graphite format.                                               
    metrics.each do |k, v|                                                         
      output [config[:scheme], k].join('.'), v                                     
    end                                                                            

    # Done                                                                         
    ok                                                                             
  end                                                                              

end 

如果语法错误实际上在此部分之前,我可以添加此之前的代码。 编辑:为每个评论请求添加完整的文件内容

完整的错误,如果有用的话:

./metrics-available-updates.rb:29: syntax error, unexpected ',', expecting keyword_end
      output [config[:scheme], k].join('.'), v

【问题讨论】:

  • output 是什么?
  • 是父类的方法。如果细节有帮助,你可以在这里看到outputgithub.com/sensu-plugins/sensu-plugin/blob/master/lib/…我继承自Graphite,所以是从第21行开始的方法。
  • 在这个具体的 sn-p 中没有语法错误。显示整个文件。
  • 添加了完整的文件内容。

标签: ruby syntax syntax-error


【解决方案1】:

如果你玩了一下,你会注意到语法错误消失了,或者当你注释掉有问题的行时,或者

output = %x[/usr/lib/update-notifier/apt-check --human-readable] 

当 Ruby 解析一个文件时,它需要猜测一个符号是表示一个方法调用,还是一个变量引用。在这种情况下,output 会作为变量存在,但再往下,你写

output [config[:scheme], k].join('.'), v

这意味着它突然变成了一个方法调用。

我承认 Ruby 词法分析器应该提供更有用的错误消息....

【讨论】:

  • 啊,早先的output 是干的(有点)!我已经注释掉了违规行,并且知道它与此有关,但我不确定。这就解释了我的代码与我在对上一个答案的评论中提到的 mongo 示例之间的区别。我正在将一段旧代码与插件“框架”合并,我没有注意到它们都使用名称 output 来表示不同的东西。我来自 Python,我确实认为 Ruby 倾向于给出非常神秘的信息。我相信我会逐渐欣赏 Ruby 在时间思维方面的优势。
【解决方案2】:

添加parentheses

...
metrics.each do |k, v|                                                         
  output([config[:scheme], k].join('.'), v)
end
...

【讨论】:

  • 我会在早上用这段代码回到机器前试一试。我觉得这个答案很有趣,因为我从一段正常运行的代码中提取了这个 each..do 循环,位于此处:github.com/sensu-plugins/sensu-plugins-mongodb/blob/master/bin/…,您会在第 120 行注意到它使用与我在代码中实现的完全相同的循环.如果您的解决方案有效,我会很好奇为什么您可以在一个中不带括号,而在另一个中不带括号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多