4 ms·
Ah well "You probably shouldn’t do that though" is just my opinion as the author. I didn't play a part in these changes. I just like Ruby enough to dig into the
by jbackus 10y ago
Ah well "You probably shouldn’t do that though" is just my opinion as the author. I didn't play a part in these changes. I just like Ruby enough to dig into their changelog :)
- psyklic 10y agoInterestingly, assignment inside conditionals is usually avoided because it is easy to accidentally do an assignment (=) rather than a comparison (==). Note that for multiple assignment this mistake can no longer happen: irb(main): a, b == 3 SyntaxError: syntax error, unexpected ==, expecting '=' Should it still be avoided? :)
- jbackus 10y agoFirst of all that is an interesting point. Given that it isn't as potentially dangerous I'll agree that it is less bad than I initially thought. I'll respond to your question by asking you a question. Which of the following expressions evaluate to "truthy"? 'truthy' if (a, b = []) # => 'truthy' if (a, b = nil) # => 'truthy' if (a, b = [nil]) # => 'truthy' if (a, b = [nil, nil]) # => 'truthy' if (a, b = [false]) # => 'truthy' if (a, b = [false, false]) # => 'truthy' if (a, b = [true, false]) # => 'truthy' if (a, b = [false, true]) # => 'truthy' if (a, b = *[]) # => 'truthy' if (a, b = *nil) # => 'truthy' if (a, b = *[nil]) # => 'truthy' if (a, b = *[nil, nil]) # => 'truthy' if (a, b = *[false]) # => 'truthy' if (a, b = *[false, false]) # => 'truthy' if (a, b = *[true, false]) # => 'truthy' if (a, b = *[false, true]) # => The answer is in this gist: https://gist.github.com/backus/c9b70dee67470698fd7d4a66ddf0384b https://gist.github.com/backus/c9b70dee67470698fd7d4a66ddf03.... Don't peek!
- psyklic 10y agoGood exercise! So it may still be too easy to misunderstand when exactly the condition will be true.
- saghm 10y agoAh, okay, I confused this with an official recommendation. Thanks for the clarification!