by russellbeattie · 8y ago · 0 comments
# bad
if v = array.grep(/foo/)
do_something(v)
# some code
end
# good
if (v = array.grep(/foo/))
do_something(v)
# some code
end
Caught a number of nasty bugs that way.[0]: https://github.com/bbatsov/ruby-style-guide#safe-assignment-...
if (2 == x)
But yes, most compilers will catch this case.
while (v = next()) { ... }
or, perhaps more usefully: while ((v = next()) != ERROR) { ... }
EDIT: just did a quick check, and LLVM did in fact warn me about this: if (x = 4) { ... }
To silence the error, you have to double up on the parenthesis, which I guess is a reasonable enough solution: if ((x = 4)) { ... }