0

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
1
  • The latter. They are assigning a value to value and then checking if it's truthy. Commented Feb 28, 2018 at 15:57

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.