George Morris
George Morris

Reputation: 484

Ruby if/assign statement?

I have run to the following code and I have no idea what it does. Why do they use = to compare values or are they assigning values and checking if the value is true after being assigned?

     if value = (key rescue nil)
       ..
     end

Upvotes: 0

Views: 54

Answers (1)

Anthony
Anthony

Reputation: 15957

This is equivalent to:

value = key rescue nil

if value
  ..  
end

or

value = begin
  key
rescue
  nil
end

if value
  ..  
end

Remember nil and false are the only two objects that are falsey in ruby and since value here could be nil, that if statement could return false.

Upvotes: 3

Related Questions