Ruby包括?投掷未定义的方法`include?' for false:FalseClass(NoMethodError)错误

时间:2014-05-01 15:25:42

标签: ruby

我基本上有以下内容:

res = "CRITICAL- Code Cache:57% Par Eden:19% Par Survivor:21% Cms Old Gen:26% Cms Perm Gen:34% | CodeCache=57% ParEden=19% ParSurvivor=21% CmsOldGen=26% CmsPermGen=34%"

if res.include? "OK"
    puts res
    exit 0
elsif res.include? "WARNING"
    puts res
    exit 1
elsif res.include? "CRITICAL"
    puts res
exit 2
end

当我跑步时,我得到了:

: undefined method `include?' for false:FalseClass (NoMethodError)

错误的退出代码。我在我的另一个脚本上使用了相同的if / elsif,并且它完美地工作,但不是在这里,唯一的区别是它不是在类中。

1 个答案:

答案 0 :(得分:0)

As per the comment

  

我的res =实际上是一个system()命令,它将另一个变量传递给bash脚本,该脚本正在为我解析数据,并返回我上面的字符串

是..那就是问题 - system如果命令提供零退出状态,则返回truefalse为< em>非零退出状态。如果命令执行失败,则返回nil 因此res将始终具有truenilfalse,从来没有一个字符串,你想要它。因此,您需要使用backtick,而不是system

相关问题