开发者

How can I refactor this if/else statement?

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 'deliver开发者_运维问答ed' (but make sure number.events actually has something)."


I am not a Ruby developer, but it seems like your number.events check in the elsif is redundant since your code will only "fall through" to that elsif in the case that number.events is NOT blank. And since you have an if...or situation, a more concise way of putting it may be:

if number.events.blank? || !number.events.ordered.first.status.downcase.include? 'delivered'
    Resque.enqueue(ProcessNumbers,number.id)


Or,

if !number.events.blank? && !number.events.ordered.first.status.downcase.include? 'delivered'
    Resque.enqueue(ProcessNumbers, number.id)

It will make sure that number.events actually has something.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜