如何在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
但是我在“,”逗号中有语法错误。
答案 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