我的条件如下:
if connection
if "name" == connection.name
...
end
end
connection
最初可能有nil
值,因此我无法检查if connection && "name" == connection.name
如何有效地简化条件?
答案 0 :(得分:8)
if connection and connection.name == "name"
...
end
答案 1 :(得分:2)
unless connection.nil? || connection.name != "name"
#...Statements
end