如何改进这个`if`声明?

时间:2015-11-30 09:50:16

标签: ruby if-statement refactoring

如何在ruby中改进此if语句?

原始if - 声明:

status = 'None available', css_class = 'grey'
if pending_final > 0
  status = 'Pending'
  css_class = 'red'
elsif requested_final == 0 && request.granted
  status = 'Granted'
  css_class = 'green'
elsif requested_final == 0 && request.granted == false
  status = 'Not requested'
  css_class = 'red'
end

我试图像这样重构:

status, css_class = if pending_final > 0
 'Pending', 'red'
elsif requested_final == 0 && request.granted
 'Granted', 'green'
 # and so on...
else
 'None available', 'grey'
end

但是我在“,”逗号中有语法错误。

1 个答案:

答案 0 :(得分:0)

试试这个

status, css_class = if pending_final > 0
 ['Pending', 'red']
elsif requested_final == 0 && request.granted
 ['Granted', 'green']
 # and so on...
else
 ['None available', 'grey']
end