我有以下列格式红宝石计数基于某些组合
"stats": {
"team": [],
"outcome": [],
"rank": []
}
我需要以确定是否存在的存在量为上述结构然后打印的东西的2个或多个结果的组合的数据。
这样的想法是:
(if stats.team.present? && if stats.outcome.present) || (if stats.outcome.present? && if stats.rank.present) || (if stats.team.present? && if stats.rank.present)
更好的方法是创建一个方法来添加计数器,它增加如果队,结果,排名具有大于0
的计数然后检查计数器大于2 例如,什么是大
def my_count
count = 0
count += 1 if stats.team.count > 0
count += 1 if stats.outcome.count > 0
count += 1 if stats.rank.count > 0
if count > 1
return true
end
end
难道这些只有2个选项或者是有一个更清洁的方式?
FYI ,Ruby中没有'++'操作符。 – squiguy