Skip to content

Commit acdf848

Browse files
committed
Merge pull request #65 from amiel/master
Add requirement of parens when using assignment in a conditional.
2 parents e359ed5 + d06c70d commit acdf848

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ You can generate a PDF or an HTML copy of this guide using
338338
end
339339
```
340340
341-
* Don't use parentheses around the condition of an `if/unless/while`.
341+
* Don't use parentheses around the condition of an `if/unless/while`,
342+
unless the condition contains an assignment (see "Using the return
343+
value of `=`" below).
342344

343345
```Ruby
344346
# bad
@@ -350,6 +352,11 @@ You can generate a PDF or an HTML copy of this guide using
350352
if x > 10
351353
# body omitted
352354
end
355+
356+
# ok
357+
if (x = self.next_value)
358+
# body omitted
359+
end
353360
```
354361

355362
* Omit parentheses around parameters for methods that are part of an
@@ -448,10 +455,18 @@ You can generate a PDF or an HTML copy of this guide using
448455
- 2
449456
```
450457
451-
* Using the return value of `=` (an assignment) is ok.
458+
* Using the return value of `=` (an assignment) is ok, but surround the
459+
assignment with parenthesis.
452460
453461
```Ruby
462+
# good - shows intented use of assignment
463+
if (v = array.grep(/foo/)) ...
464+
465+
# bad
454466
if v = array.grep(/foo/) ...
467+
468+
# also good - shows intended use of assignment and has correct precedence.
469+
if (v = self.next_value) == "hello" ...
455470
```
456471
457472
* Use `||=` freely to initialize variables.

0 commit comments

Comments
 (0)