Hello,
when looking at the parse.y code in Ruby 1.8.5, I noticed following
lines (lines 1131-1139):
| tUPLUS arg
{
if ($2 && nd_type($2) == NODE_LIT) {
$$ = $2;
}
else {
$$ = call_op($2, tUPLUS, 0, 0);
}
}
The code obviously "eats" the unary plus in front of literals, so in
cases like "+42" no "+@" method is called. I suppose this is an
optimization.
However, the "nd_type($2) == NODE_LIT" condition holds true not only for
numeric literals, but also e.g. simple regexps. This results in "eating"
the unary plus in places where it seems wrong to me:
$ ruby -ve 'puts(+/regexp/)'
ruby 1.8.5 (2006-08-25) [i486-linux]
(?-mix:regexp)
$ ruby -ve 'puts(+(/regexp/))'
ruby 1.8.5 (2006-08-25) [i486-linux]
-e:1: undefined method `+@' for /regexp/:Regexp (NoMethodError)
I think that "+/regexp/" expression should raise NoMethodError too.
-----
I tried current trunk (compiled from SVN) and it confused me more, as
the exception was not raised even when the unary plus was separated from
the regexp by parens:
$ ./ruby -ve 'puts(+/regexp/)'
ruby 1.9.0 (2007-09-28 patchlevel 0) [i686-linux]
(?-mix:regexp)
$ ./ruby -ve 'puts(+(/regexp/))'
ruby 1.9.0 (2007-09-28 patchlevel 0) [i686-linux]
(?-mix:regexp)
-----
Is the described behavior and the differences between 1.8.x and trunk
correct or it is a bug? Thanks for clarification.
David Majda
--
Everyone gets everything he wants. --Captain Willard in Apocalypse Now
Personal :: david / majda.cz :: www.majda.cz
Work :: david.majda / impaladesign.cz :: www.impaladesign.cz