【问题标题】:Ruby lint error what is the right way to write this with a guard clauseRuby lint 错误用保护子句写这个的正确方法是什么
【发布时间】:2015-08-18 17:44:09
【问题描述】:

尝试运行命令,如果成功则继续,如果失败则引发错误并将命令输出发送到控制台。

output = `#{command}`
unless $CHILD_STATUS.success?
  raise "#{command} failed with:\n#{output}"
end

C: Use a guard clause instead of wrapping the code inside a conditional expression.

代码运行正常,但是 rubocop 不喜欢它。什么是改进此代码样式并仍为我提供相同功能的最佳方法?

【问题讨论】:

  • 这是在方法内部吗?我们需要更多上下文来修改它以满足 Rubocup 的建议。

标签: ruby lint rubocop


【解决方案1】:

试试这个

raise "#{command} failed with:\n#{output}" unless $CHILD_STATUS.success?

【讨论】:

  • 好的,我不知道为什么,但我试图将输出分配强制到 unless 语句中。我想我不太明白 rubocop/style 建议在说什么。不过这样干净多了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-31
  • 1970-01-01
  • 2017-02-16
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
相关资源
最近更新 更多