[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How +:symbol is parsed

Victor 'Zverok' Shepelev

8/20/2007 4:39:00 PM

Hi.

I wanted to be evil and add my own custom semantics to +:some_symbol
constructs. But I've found such consctructs doesn't call Symbol#+@, while
+(:some_symbol) does call.

I can suppose it's reason of numbers construction in parser

+3 #doesn't calls Fixnum#+@, it's like Finxum.new(+3),
+(3) #does calls Fixnum#+@, it's like Fixnum.new(3).+@

but it's still strange for me this rule works for symbols too (but not for
strings, +"test" calls String#+@). Can somebody please explain this
strangeness?

Thanks.

V.


1 Answer

Alex LeDonne

8/20/2007 10:22:00 PM

0

On 8/20/07, Victor Zverok Shepelev <vshepelev@imho.com.ua> wrote:
> Hi.
>
> I wanted to be evil and add my own custom semantics to +:some_symbol
> constructs. But I've found such consctructs doesn't call Symbol#+@, while
> +(:some_symbol) does call.
>
> I can suppose it's reason of numbers construction in parser
>
> +3 #doesn't calls Fixnum#+@, it's like Finxum.new(+3),
> +(3) #does calls Fixnum#+@, it's like Fixnum.new(3).+@
>
> but it's still strange for me this rule works for symbols too (but not for
> strings, +"test" calls String#+@). Can somebody please explain this
> strangeness?
>
> Thanks.
>
> V.
>

Well, I haven't looked at parse.y, but I'll wager that the difference
between 3 and "test" is that, for numeric literals, a preceding + or -
is considered the sign of the literal number. That is, +3 is a number
all by itself, whereas +"test" doesn't make sense unless the + is a
method call. There's also no method call when a number is in
exponential notation like -4.3e+11 .

In ruby-ancient times, a symbol was more like an integer, which may or
may not explain the +:symbol semantics.

-A