Do you leave parentheses in or out in Ruby? [close

2019-01-13 05:19发布

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

9条回答
▲ chillily
2楼-- · 2019-01-13 05:52

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.
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-13 05:54

Whichever is more readable usually.

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

查看更多
The star\"
4楼-- · 2019-01-13 05:54

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.

查看更多
登录 后发表回答