Stefan Matthias Aust <sma / 3plus4.de> writes:

> First a question: Why is
> 
>  p (1..10).to_a
> 
> interpreted as
> 
>  (p (1..10)).to_a    >>  1..10
> 
> instead of
> 
>  p ((1..10).to_a)  >>  [1, ..., 10]
> 
> It gave me a hard time to figure this out, especially as "nil.to_a"
> (nil is the result of p(..)) has a meaning and doesn't throw an error.
> 
> I would have expected that, if I leave a SPACE between the method name
> and the open parenthesis, it is NOT taken as the argument to "p" but
> instead the "." has a higher precedence.  At least this would be a
> useful assumption IMHO.

The problem is, there are many (obviously sick and misguided) folks who
leave a space between the name of the message and the open parenthesis
which starts the parameter list. Using space to disambiguate the
precedence would then leave them feeling upset.

In general, it's easy to argue both side of this:

   p (1..10).to_a

should naturally be p((1..10).to_a), but

   sin(.7).to_i

should equally naturally be

   (sin(.7)).to_i

In the end, I'd apply Thomas' First Ruby Pragma: always use
parentheses around method arguments unless the arguments are trivial.



Regards


Dave