This is correct. + or - at the start of an numeric literal is
tokenized as part of that literal. (Except in the ** case, as you say.)
I think that your post is more correct than mine: when I put that bit
about (-7**2) I was assuming (without trying) that (+7**2)
would behave similarly. Not so, and your comment below
about "+" being eaten up is correct.
The situation is somewhat better in 1.9, where at least
=A0+ 7 #with a space in between
is treated as +@ method applied to the literal 7. But that doesn't
help in 1.8. Note that in ruby 1.8, any number of leading + signs are
glommed into the integer literal:
=A0++++++++++++++++++++++++++7 =A0#still no method call in 1.8
Having integer literals eat up the leading + or - as part of the
number token is a common 'optimization' in many languages...
When all else fails, actually try some code.
Which is what I should have done before my first post.
As confirmation of what you say in your post:
# ruby=3D1.9.1 release-date=3D2009-07-16 platform=3Di386-mingw32
class Fixnum
def +@() ; puts "in +@" ; self ; end
def -@() ; puts "in -@" ; self * -1 ; end
end
-7 #=3D> -7
+7 #=3D> 7
-(7) #=3D> in -@ #=3D> -7
+(7) #=3D> in +@ #=3D> 7
-7**2 #=3D> in -@ #=3D> -49
+7**2 #=3D> 49 # note difference between this and -7**2
---7 #=3D> in -@ #=3D> in -@ #=3D> -7
- 7 #=3D> in -@ #=3D> -7
+++7 #=3D> in +@ #=3D> in +@ #=3D> 7
+ 7 #=3D> in +@ #=3D> 7
# ruby=3D1.8.6 release-date=3D2007-09-24 platform=3Di386-mswin32
# the only differences are, as you said:
+++7 #=3D> 7
+ 7 #=3D> 7