Do you leave parentheses in Ruby?

When is this possible ... do you leave parentheses in or in Ruby?

+82
ruby coding-style
Dec 04 '08 at 13:46
source share
9 answers

From Ruby Style Elements

Ruby allows you to eliminate parentheses, in general, resist this temptation.

Brackets make code easier to follow. The general Ruby style is intended to be used with the exception of the following cases:

  • Always leave blank parentheses
  • Brackets can be excluded from a single command, surrounded by ERb delimiters - ERb tokens make sure the code is still readable.
  • A string, which is the only command and the only simple argument, can be written without parentheses. Personally, I think I do it less and less, but it is still well readable. I do not like the loneliness of a string in regular ruby โ€‹โ€‹code, which has several arguments and parentheses.
  • Many Ruby Domain-based languages โ€‹โ€‹(such as Rake) do not use parentheses to preserve the more natural language they feel for their statements.
+91
Dec 04 '08 at 23:51
source share

I use parenos as comments to help the future me ... which is likely to have fewer brain cells than I have now :-)

There is nothing worse than looking at some code that you wrote 2 years ago, and it is wrong to understand it so that you break something during its modification.

If the guys save me in the future for a few minutes (or hours) in the future, I will add as much as I need to make the expression crystal clear.

+26
Dec 08 '08 at 15:52
source share

I leave them when I do DSL-ish, for example t.column or has_many in rails. The rest of the time it usually comes down to clarity, and it's probably even split.

+9
Dec 04 '08 at 14:56
source share

I think I do both, but I definitely support them if it adds readability and avoids statements that look ambiguous.

+8
Dec 04 '08 at 13:52
source share

If you mean function calls, I always put parentheses because they are always easier to read. If you mean in terms (if, at the time), I only make brackets when they are needed.

+7
04 Dec '08 at 13:54
source share

I try to leave them, if at all possible. I think this makes it easier to read the code (generally speaking).

+7
Dec 04 '08 at 13:59
source share

Whatever is more readable usually.

But I always use parentheses when I insert function calls inside other parameters

+3
Dec 04 '08 at 13:53
source share

I try to leave them when executing statements like assert_equal. Perhaps this will make it a language-specific language.

+2
Dec 08 '08 at 1:49
source share

If you programmed for a long time, you will probably have an โ€œitchโ€ to add parentheses, and in many cases there are good reasons for this.

The code is easier on the eyes, although, in my opinion, I have not encountered a problem yet - if you need parentheses, you will know this before you have to run a script for debugging.

+1
Jul 11 2018-12-12T00:
source share



All Articles