条件评估的条件评估

时间:2015-10-06 19:55:21

标签: ruby syntax conditional

表达这段代码可能是一种更简洁,更简洁的方式吗?

if should_be_admin
  result = user and user.admin?
else
  result = not user.nil?
end

1 个答案:

答案 0 :(得分:2)

result = should_be_admin ? (user and user.admin?) : !!user

result = user ? (should_be_admin ? user.admin? : true) : false

!!将真实值转换为true,将虚假值(nilfalse)转换为false