Ruby parens-free function calls [was Re: Accessing parent objects]

Python python at bladeshadow.org
Mon Mar 26 15:43:05 EDT 2018


On Sun, Mar 25, 2018 at 10:33:49AM -0700, Rick Johnson wrote:
> > [steve at ando ruby]$ ruby ws-example.rb
> > a + b => 7
> > a+b   => 7
> > a+ b  => 7
> > a +b  => 3
> >
> > Here's the source code:
> >
> > # --- cut ---
> > def a(x=4)
> >     x+2
> > end
> >
> > b = 1
> > print "a + b => ", (a + b), "\n"
> > print "a+b   => ", (a+b), "\n"
> > print "a+ b  => ", (a+ b), "\n"
> > print "a +b  => ", (a +b), "\n"
> > # --- cut ---
> 
> 
> Short of demonstrating that you have knack for writing
> obfuscated code :-), i don't see the point here. 

I think the point is 7 != 3...

> Here, i took the liberty of un-obfuscating your code [...]

Not to me, you didn't.  While I don't know ruby, I already got from
context that "a" was a function call, including without the parens,
albeit annoying to read.  I've previously seen that in Perl, and never
liked it there either, because you can't immediately tell by looking
at it whether it is a variable or a function call.  While I think that
itself speaks to Stephen's consistency point, I don't think that's too
controvercial.

But what I don't get is how the last print statement prints 3.  There
seems to be some kind of magic happening related to the positioning of
the + relative to the operand(s).  That's neither consistent nor
intuitive.  Or, it's some other thing caused by a detail I overlooked,
but if so your "unobfuscated" version did nothing to clarify what that
detail is, even after scrutinizing it somewhat closely.  In either
case, I can't imagine how Ruby arrives at the answer 3.




More information about the Python-list mailing list