On 11/4/06, Joel VanderWerf <vjoel / path.berkeley.edu> wrote:
> Jacob Fugal wrote:
> >> Due to the way ruby interprets formula's, squaring a negative number
> >> isn't possible unless you use brackets. This caught me unaware, and
> >> might be counterintuitive for more people.
> >>
> >> irb(main):018:0> -2**2
> >> => -4
> >> irb(main):019:0> (-2)**2
> >> => 4
> >
> > The "problem" lies in the confluence of precedence with the syntax of
> > literals. It should be obvious that exponentiation (**) binds with a
> > higher precedence than unary negation /as an operation/, because
> > exponentiation has precedence of multiplication (and unary negation is
> > essentially multiplication by -1). The confusion is because there's a
> > misconception the the "-" in "-2" is part of the literal when it is
> > not -- it is an operation applied to the object derived from the
> > literal "2".
>
> Any yet
>
> irb(main):002:0> -2.abs
> => 2
>
> So there are cases where the operation of "concatenating characters to
> form a literal" has higher priority than an operation on objects.
>
> It's not simply a matter of `-' having priority over `.', as can be seen
> from this example:
>
> irb(main):006:0> x=2
> => 2
> irb(main):007:0> -x.abs
> => -2
>
> So "dot" does have priority over "unary minus", but not over literal
> formation.
>
> Why shouldn't literals always take precedence? Does it beak too many
> habits from ancestor languages (perl, as pointed out)? Is it too hard to
> parse?

Good points, I don't know.

Jacob Fugal