简体   繁体   中英

Ruby conditional statement

I have a condition like:

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

connection may have nil value initially, so I am not able to check if connection && "name" == connection.name

How can I simplify the condition efficiently?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM