Sayuj
Sayuj

Reputation: 7622

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?

Upvotes: 2

Views: 304

Answers (2)

sawa
sawa

Reputation: 168269

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

Upvotes: 8

Mithun Sasidharan
Mithun Sasidharan

Reputation: 20950

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

Upvotes: 2

Related Questions