Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add requirement of parens when using assignment in a conditional.

Closes #53.
  • Loading branch information...
commit d06c70d9dd7517e21e1f9077e8d35297f63d7ee1 1 parent 299d937
Amiel Martin amiel authored

Showing 1 changed file with 17 additions and 2 deletions. Show diff stats Hide diff stats

  1. +17 2 README.md
19 README.md
Source Rendered
@@ -338,7 +338,9 @@ You can generate a PDF or an HTML copy of this guide using
338 338 end
339 339 ```
340 340
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).
342 344
343 345 ```Ruby
344 346 # bad
@@ -350,6 +352,11 @@ You can generate a PDF or an HTML copy of this guide using
350 352 if x > 10
351 353 # body omitted
352 354 end
  355 +
  356 + # ok
  357 + if (x = self.next_value)
  358 + # body omitted
  359 + end
353 360 ```
354 361
355 362 * 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
448 455 - 2
449 456 ```
450 457
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.
452 460
453 461 ```Ruby
  462 + # good - shows intented use of assignment
  463 + if (v = array.grep(/foo/)) ...
  464 +
  465 + # bad
454 466 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" ...
455 470 ```
456 471
457 472 * Use `||=` freely to initialize variables.

0 comments on commit d06c70d

Please sign in to comment.
Something went wrong with that request. Please try again.