From: "David A. Black" Date: 2004-08-19T21:18:55+09:00 Subject: Re: When to use parentheses around method args? Hi -- On Wed, 18 Aug 2004, Richard Dale wrote: > My problem was the the method: > > def << aNode > @children << aNode > end > > I wondered what is this strange new ruby syntax? But as soon as I saw it > meant this I could follow the code: > > def <<(aNode) > @children << aNode > end > > It's like one of the 'Escher mind teasers' where you seen a cube from one > perspective, and then suddenly your brain flip-flops to another one. > > Are there any rules of thumb for when it's a good idea to leave out the > brackets and when to use them? Personally I like to adhere as much as possible to the style established by Matz and the other core developers, though I know this attitude has lost popularity in recent years. Anyway, if you want to be a conformist sheep like me, you can use the Ruby distribution as a source of information about coding style: # These are almost certainly not 100% correct counts, but they give # you a very good indication of standard practice. This is # Ruby 1.8.1. # All def's: $ grep "^[[:space:]]*def" `find . -name "*.rb"` | wc -l 7484 # All the def's with no parentheses: $ grep "^[[:space:]]*def " `find . -name "*.rb"` | cut -d# -f1 |\ grep -v "(" | wc -l 2438 # All of the above which have more than one word other than "def" # (filtering out one-line defs): $ grep "^[[:space:]]*def" `find . -name "*.rb"` | cut -d# -f1 | \ grep -v "(" | grep -v "end$"| ruby -ne "print if split.size > 3" | wc -l 160 So about 94% of all def's with arguments in the Ruby distribution use parentheses. You'll find similar stats for camelCase method/variable names, indentation by more than two spaces, and other points of style where Ruby will accept non-conventional practice but conventions and traditions do exist. How you deal with these things is up to you. David -- David A. Black dblack@wobblini.net