90

When possible.. do you leave parentheses in or out in Ruby?

9 Answers 9

104

From the Elements of Ruby Style

Ruby allows you to leave out parenthesis, in general, resist this temptation.

Parenthesis make the code easier to follow. General Ruby style is to use them, except in the following cases:

  • Always leave out empty parentheses
  • The parentheses can be left out of a single command that is surrounded by ERb delimiters -- the ERb markers make sure the code is still readable
  • A line that is a single command and a single simple argument can be written without the parenthesis. Personally, I find that I do this less and less, but it's still perfectly readable. I tend not to like single lines in regular ruby code that have multiple arguments and no parentheses.
  • A lot of Ruby-based Domain Specific Languages (such as Rake) don't use parenthesis to preserve a more natural language feel to their statements.
2
  • The exceptions are so numerous, it would be easier to DEFAULT to omitting parens. The exceptions are far fewer: Only involve parens for nested calls.
    – mcandre
    Aug 11, 2021 at 17:35
  • That might be logical for many developers which a strong usage of other languages. I understand that someone that usually works with JavaScript (just one example) finds more readable to use always parentheses, even on the cases where according to the Ruby syntax is not mandatory. Though in many cases, when it comes to developers that entirely or most of the time are working with Ruby (many RoR Developers, like in my case)... then for many of them it's more readable without the parentheses. It's just a matter of opinion I guess. I prefer it without them. Feb 1, 2022 at 22:11
28

I use parens as comments to help the future me... who is likely to have fewer brain cells than the current me :-)

Nothing worse than looking at some code you wrote 2 years ago and misunderstanding it, so that you break something while modifying it.

If parens will save the future me a few minutes (or hours) in the future, I'll put in as many as needed to make the statement crystal clear.

0
9

I leave them out when I'm doing DSL-ish stuff, like t.column or has_many in rails. The rest of the time, it generally comes down to clarity, and it's probably an even split.

8

I guess I do both, but I definitely keep them in if it adds to readability and avoids statements that look ambiguous.

8

If you mean in function calls, I always put parenthesis because it's always easier to read. If you mean in conditions (if, while) I only put parenthesis when they're necessary.

1
  • 3
    I agree. In php for example I can quickly spot a var by the $ prefix.. in javascript I can reconize a function by the parenthesis(). In Ruby the difference between a var or func (without parenthesis) is not always easy to see.
    – CharlesChipy
    Dec 4, 2008 at 14:55
8

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

4

Whichever is more readable usually.

But I always use parentheses when I'm nesting function calls inside other ones' parameters

2

I tend to leave them out when doing assertions such as assert_equal. Maybe it's to make it domain specific language-like.

1

If you've been programming for a long time, you'll probably have an "itch" to add parentheses, and in many cases there are good reasons for this.

The code is easier on the eyes though in my opinion, and I haven't run into a problem yet--if you're going to need parentheses, you'll know it beforehand before you have to run into the debugging script.

6
  • 4
    "My teacher tells me its inevitable". It is, and can be hard to debug. I recommend using them to avoid ambiguous parameter assignment. Oct 17, 2012 at 22:06
  • 2
    speaking to the non-parens crowd i ran into this problem the other day if owner.is_a? thing //worked fine if owner.is_a? thing && x > 1 //not fine i've only been learning ruby for a couple weeks now and where i work uses the smallest amount of characters possible and if you come from any other language, there's a learning curve to know when you're passing an implictiy hash, an array of symbols, passing to symbols to a function...i'm not a fan.
    – Mega Man
    Nov 8, 2016 at 16:54
  • @MegaMan if owner.is_a? thing and x > 1
    – anna328p
    Feb 6, 2017 at 3:56
  • 1
    @DmitryKudriavtsev and does not have the same operator precedence that && has
    – Mega Man
    Feb 6, 2017 at 19:02
  • @MegaMan That's the point. The lower precedence allows it to work.
    – anna328p
    Feb 7, 2017 at 20:43