I’ve seen this a couple times :
if foo = bar
# do something
end
Is this syntactically appropriate? Does it differ from? :
if foo == bar
# do something
end
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
it can be read as this:
Assignment operator in ruby returns a value that was assigned. This makes it very useful in some situations, thanks to the fact that ruby has very simple rules about what’s true and what’s not.
Here’s a typical example where assignment is used in a condition (not a good one, though, because with files you should use
each_lineand a block, but you get the idea):This, on the other hand, is a comparison operator, not assignment operator. It returns a boolean value if
fooandbarwere equal.[edit] An example that demonstrates that using this pattern may have a real impact on the verbosity of code. Look at this nested conditional expression:
Using this pattern we can write instead (note how I surround the assignments):