如何重构这个 if/else 语句?
下面是 if/else 语句:
if number.events.blank?
Resque.enqueue(ProcessNumbers, number.id)
elsif number.events && !number.events.ordered.first.status.downcase.include? 'delivered'
Resque.enqueue(ProcessNumbers, number.id)
end
最终想说的是:“如果 number.events
为空,则运行 enqueue
。或者 if number.events.ordered.first。 status.downcase.include
包含“已交付”(但要确保 number.events
实际上有某些内容)。”
Here's the if/else statement:
if number.events.blank?
Resque.enqueue(ProcessNumbers, number.id)
elsif number.events && !number.events.ordered.first.status.downcase.include? 'delivered'
Resque.enqueue(ProcessNumbers, number.id)
end
Ultimately trying to say: "If number.events
is blank, run enqueue
. Or if number.events.ordered.first.status.downcase.include
includes 'delivered' (but make sure number.events
actually has something)."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 Ruby 开发人员,但您在
elsif
中的number.events
检查似乎是多余的,因为您的代码只会“落入”该elsif
在number.events
不为空的情况下。由于您遇到了if...or
情况,更简洁的表达方式可能是:I am not a Ruby developer, but it seems like your
number.events
check in theelsif
is redundant since your code will only "fall through" to thatelsif
in the case thatnumber.events
is NOT blank. And since you have anif...or
situation, a more concise way of putting it may be:或者,
它将确保
number.events
实际上有一些东西。Or,
It will make sure that
number.events
actually has something.