Ruby条件语句

时间:2012-01-03 06:53:24

标签: ruby-on-rails ruby string oop conditional-statements

我的条件如下:

if connection
  if "name" == connection.name
    ...
  end
end

connection最初可能有nil值,因此我无法检查if connection && "name" == connection.name

如何有效地简化条件?

2 个答案:

答案 0 :(得分:8)

if connection and connection.name == "name"
   ...
end

答案 1 :(得分:2)

unless connection.nil? || connection.name != "name"
  #...Statements
end