Strange whitespace parsing behavior on Ruby 1.8.7 (patchlevel249/302)

E

Ehsanul Hoque

An issue pops up when I use a method's integer return value in a function a=
rgument while performing arithmetic on that value. Then missing whitespace =
between the arithmetical operator and the second argument can cause random =
errors=2C but only if there is whitespace preceding the operator. I don't w=
ant to rush to judgement and call this a bug=2C but it certainly seems like=
one=2C hidden till now because you have have formatting that's slightly ug=
ly for the error to turn up. Here's an irb session that demonstrates the is=
sue:
irb(main):062:0* x =3D '123456'=3D> "123456"irb(main):063:0> x.slice(1=2C x=
size -2)SyntaxError: compile error(irb):63: syntax error=2C unexpected ')'=
=2C expecting kDO_BLOCK from (irb):63 from :0irb(main):064:0>=
x.slice(1=2C x.size - 2)=3D> "2345"irb(main):065:0> x.slice(1=2C x.size-2)=
=3D> "2345"irb(main):066:0> x.slice(1=2C x.size -2)SyntaxError: compile err=
or(irb):66: syntax error=2C unexpected ')'=2C expecting kDO_BLOCK fr=
om (irb):66 from :0irb(main):067:0> x[x.size -3=2C1]ArgumentError: w=
rong number of arguments (2 for 0) from (irb):67:in `size' fr=
om (irb):67 from :0irb(main):068:0> x[x.size - 3=2C1]=3D> "4"irb(mai=
n):069:0> def ok(a=2Cb)irb(main):070:1> end=3D> nilirb(main):071:0> ok(x.si=
ze +3=2C4)ArgumentError: wrong number of arguments (2 for 0) from (i=
rb):71:in `size' from (irb):71 from :0irb(main):072:0> ok(4=
=2Cx.size +3)SyntaxError: compile error(irb):72: syntax error=2C unexpected=
')'=2C expecting kDO_BLOCK from (irb):72 from :0irb(main):07=
4:0> ok(4=2Cx.size + 3)=3D> nil
=
 
R

Robert Klemme

An issue pops up when I use a method's integer return value in a function=
argument while performing arithmetic on that value. Then missing whitespac=
e between the arithmetical operator and the second argument can cause rando=
m errors, but only if there is whitespace preceding the operator. I don't w=
ant to rush to judgement and call this a bug, but it certainly seems like o=
ne, hidden till now because you have have formatting that's slightly ugly f=
or the error to turn up. Here's an irb session that demonstrates the issue:
irb(main):062:0* x =3D '123456'=3D> "123456"irb(main):063:0> x.slice(1, x=
size -2)SyntaxError: compile error(irb):63: syntax error, unexpected ')', =
expecting kDO_BLOCK =A0 =A0 =A0 =A0from (irb):63 =A0 =A0 =A0 =A0from :0irb(=
main):064:0> x.slice(1, x.size - 2)=3D> "2345"irb(main):065:0> x.slice(1, x=
size-2)=3D> "2345"irb(main):066:0> x.slice(1, x.size -2)SyntaxError: compi=
le error(irb):66: syntax error, unexpected ')', expecting kDO_BLOCK =A0 =A0=
=A0 =A0from (irb):66 =A0 =A0 =A0 =A0from :0irb(main):067:0> x[x.size -3,1]=
ArgumentError: wrong number of arguments (2 for 0) =A0 =A0 =A0 =A0from (irb=
):67:in `size' =A0 =A0 =A0 =A0from (irb):67 =A0 =A0 =A0 =A0from :0irb(main)=
:068:0> x[x.size - 3,1]=3D> "4"irb(main):069:0> def ok(a,b)irb(main):070:1>=
end=3D> nilirb(main):071:0> ok(x.size +3,4)ArgumentError: wrong number of =
arguments (2 for 0) =A0 =A0 =A0 =A0from (irb):71:in `size' =A0 =A0 =A0 =A0f=
rom (irb):71 =A0 =A0 =A0 =A0from :0irb(main):072:0> ok(4,x.size +3)SyntaxEr=
ror: compile error(irb):72: syntax error, unexpected ')', expecting kDO_BLO=
CK =A0 =A0 =A0 =A0from (irb):72 =A0 =A0 =A0 =A0from :0irb(main):074:0> ok(4=
,x.size + 3)=3D> nil
Can you repost with proper indentation and line wrapping? Guessing
from your text I assume you might have fallen into a unary / binary
operator ambiguity.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
C

Colin Bartlett

[Note: parts of this message were removed to make it a legal post.]

Can you repost with proper indentation and line wrapping?
Guessing from your text I assume you might have fallen
into a unary / binary operator ambiguity.

That seems to be a correct diagnosis. See below for the OP's irb session
with formatting.
For the OP:

7.abs + 3 #=> 10
7.abs+3 #=> 10

7.abs +3 #=> ArgumentError: wrong number of arguments(1 for 0)
## Ruby parses the "+" in " +3" as a unary + on 3 giving integer 3,
## which means that integer 3 is an argument to abs, hence the error
message.

def okp( a, b ); a + b; end #=> nil

okp(4, 7.abs + 3) #=> 14
okp(4, 7.abs+3) #=> 14
okp(4, 7.abs +3) #=> SyntaxError: (irb):23: syntax error,
# unexpected tInteger, expecting ")"
## I leave that - and the next - one to Robert to explain!

x = '123456' #=> "123456"
x.slice(1, x.size -2) #=> SyntaxError: compile error(irb):63:
# syntax error, unexpected ')',
# expecting kDO_BLOCK
# from (irb):63 from :0
## Again, the "-" in " -2" is parsed on a unary on 2 giving integer value
-2.
## Why the ')' is unexpected I leave to Robert!


*** the OP's irb session

irb(main):062:0* x = '123456'=> "123456"

irb(main):063:0> x.slice(1, x.size -2)
SyntaxError: compile error(irb):63: syntax error, unexpected ')',
expecting kDO_BLOCK
from (irb):63 from :0

irb(main):064:0> x.slice(1, x.size - 2)=> "2345"

irb(main):065:0> x.slice(1, x.size-2)=> "2345"

irb(main):066:0> x.slice(1, x.size -2)
SyntaxError: compile error(irb):66: syntax error, unexpected ')',
expecting kDO_BLOCK
from (irb):66 from :0

irb(main):067:0> x[x.size -3,1]
ArgumentError: wrong number of arguments (2 for 0)
from (irb):67:in `size' from (irb):67 from :0

irb(main):068:0> x[x.size - 3,1]=> "4"

irb(main):069:0> def ok(a,b)
irb(main):070:1> end=> nil

irb(main):071:0> ok(x.size +3,4)
ArgumentError: wrong number of arguments (2 for 0)
from (irb):71:in `size' from (irb):71 from :0

irb(main):072:0> ok(4,x.size +3)
SyntaxError: compile error(irb):72: syntax error, unexpected ')',
expecting kDO_BLOCK
from (irb):72 from :0

irb(main):074:0> ok(4,x.size + 3)=> nil
 
R

Robert Klemme

That seems to be a correct diagnosis. See below for the OP's irb session
with formatting.
For the OP:

7.abs + 3 #=3D> 10
7.abs+3 =A0 #=3D> 10

7.abs +3 =A0 #=3D> ArgumentError: wrong number of arguments(1 for 0)
## Ruby parses the "+" in " +3" as a unary + on 3 giving integer 3,
## which means that integer 3 is an argument to abs, hence the error
message.

def okp( a, b ); a + b; end =A0#=3D> nil

okp(4, 7.abs + 3) #=3D> 14
okp(4, 7.abs+3) =A0 #=3D> 14
okp(4, 7.abs +3) =A0#=3D> SyntaxError: (irb):23: syntax error,
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# =A0 unexpected tInte= ger, expecting ")"
## I leave that - and the next - one to Robert to explain!

x =3D '123456' =A0#=3D> "123456"
x.slice(1, x.size -2) =A0#=3D> SyntaxError: compile error(irb):63:
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # syntax error, u= nexpected ')',
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # expecting kDO_B= LOCK
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # from (irb):63 f= rom :0
## Again, the "-" in " -2" is parsed on a unary on 2 giving integer value
-2.
## Why the ')' is unexpected I leave to Robert!

Very generous. :)

Concluding from systematic experimentation I believe both errors you
left for me are related to the fact how Ruby parses (or rather: tries
to parse) in absence of brackets:

14:53:27 $ ruby19 -ce 'a(b.c +1)'
Syntax OK
14:53:45 $ ruby19 -ce 'a(d,b.c +1)'
-e:1: syntax error, unexpected tINTEGER, expecting ')'
a(d,b.c +1)
^
14:53:50 $ ruby19 -ce 'a(b.c +1,e)'
Syntax OK
14:53:55 $ ruby19 -ce 'a(d,b.c(+1))'
Syntax OK
14:54:08 $ ruby19 -ce 'a((d,b.c)+1)'
-e:1: syntax error, unexpected ')', expecting '=3D'
a((d,b.c)+1)
^
-e:1: syntax error, unexpected $end, expecting ')'
14:59:30 $ ruby19 -ce 'a(d,(b.c) +1)'
Syntax OK

It seems that 14:53:45 is parsed like 14:54:08 which is apparently is
illegal because you also cannot do this:

14:54:16 $ ruby19 -ce '(a,b)'
-e:1: syntax error, unexpected '\n', expecting '=3D'

14:58:18 $ ruby19 -ce '(d,b.c +1)'
-e:1: syntax error, unexpected tINTEGER, expecting tCOLON2 or '[' or '.'
(d,b.c +1)
^

Hope that sheds some light.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
E

Ehsanul Hoque

on argument while performing arithmetic on that value. Then missing whitesp=
ace between the arithmetical operator and the second argument can cause ran=
dom errors=2C but only if there is whitespace preceding the operator. I don=
't want to rush to judgement and call this a bug=2C but it certainly seems =
like one=2C hidden till now because you have have formatting that's slightl=
y ugly for the error to turn up. Here's an irb session that demonstrates th=
e issue:=2C x.size -2)SyntaxError: compile error(irb):63: syntax error=2C unexpecte=
d ')'=2C expecting kDO_BLOCK =A0 =A0 =A0 =A0from (irb):63 =A0 =A0 =A0 =A0fr=
om :0irb(main):064:0> x.slice(1=2C x.size - 2)=3D> "2345"irb(main):065:0> x=
slice(1=2C x.size-2)=3D> "2345"irb(main):066:0> x.slice(1=2C x.size -2)Syn=
taxError: compile error(irb):66: syntax error=2C unexpected ')'=2C expectin=
g kDO_BLOCK =A0 =A0 =A0 =A0from (irb):66 =A0 =A0 =A0 =A0from :0irb(main):06=
7:0> x[x.size -3=2C1]ArgumentError: wrong number of arguments (2 for 0) =A0=
=A0 =A0 =A0from (irb):67:in `size' =A0 =A0 =A0 =A0from (irb):67 =A0 =A0 =
=A0 =A0from :0irb(main):068:0> x[x.size - 3=2C1]=3D> "4"irb(main):069:0> de=
f ok(a=2Cb)irb(main):070:1> end=3D> nilirb(main):071:0> ok(x.size +3=2C4)Ar=
gumentError: wrong number of arguments (2 for 0) =A0 =A0 =A0 =A0from (irb):=
71:in `size' =A0 =A0 =A0 =A0from (irb):71 =A0 =A0 =A0 =A0from :0irb(main):0=
72:0> ok(4=2Cx.size +3)SyntaxError: compile error(irb):72: syntax error=2C =
unexpected ')'=2C expecting kDO_BLOCK =A0 =A0 =A0 =A0from (irb):72 =A0 =A0 =
=A0 =A0from :0irb(main):074:0> ok(4=2Cx.size + 3)=3D> nil
=20
Can you repost with proper indentation and line wrapping? Guessing
from your text I assume you might have fallen into a unary / binary
operator ambiguity.
=20
Kind regards
=20
robert
=20
--=20
remember.guy do |as=2C often| as.you_can - without end
http://blog.rubybestpractices.com/
=20

Sorry about that=2C Hotmail's "rich text" formatting messed it up I believe=
Here it goes again with plain text:
irb(main):062:0* x =3D '123456'=3D> "123456"irb(main):063:0> x.slice(1=2C x=
size -2)SyntaxError: compile error(irb):63: syntax error=2C unexpected ')'=
=2C expecting kDO_BLOCK=A0=A0 =A0 =A0 =A0from (irb):63=A0=A0 =A0 =A0 =A0fro=
m :0 =A0irb(main):064:0> x.slice(1=2C x.size - 2)=3D> "2345"irb(main):065:0=
x.slice(1=2C x.size-2)=3D> "2345"irb(main):066:0> x.slice(1=2C x.size -2)=
SyntaxError: compile error(irb):66: syntax error=2C unexpected ')'=2C expec=
ting kDO_BLOCK=A0=A0 =A0 =A0 =A0from (irb):66=A0=A0 =A0 =A0 =A0from :0 =A0i=
rb(main):067:0> x[x.size -3=2C1]ArgumentError: wrong number of arguments (2=
for 0)=A0=A0 =A0 =A0 =A0from (irb):67:in `size'=A0=A0 =A0 =A0 =A0from (irb=
):67=A0=A0 =A0 =A0 =A0from :0 =A0irb(main):068:0> x[x.size - 3=2C1]=3D> "4"=
irb(main):069:0> def ok(a=2Cb)irb(main):070:1> end=3D> nilirb(main):071:0> =
ok(x.size +3=2C4)ArgumentError: wrong number of arguments (2 for 0)=A0=A0 =
=A0 =A0 =A0from (irb):71:in `size'=A0=A0 =A0 =A0 =A0from (irb):71=A0=A0 =A0=
=A0 =A0from :0 =A0irb(main):072:0> ok(4=2Cx.size +3)SyntaxError: compile e=
rror(irb):72: syntax error=2C unexpected ')'=2C expecting kDO_BLOCK=A0=A0 =
=A0 =A0 =A0from (irb):72=A0=A0 =A0 =A0 =A0from :0 =A0irb(main):074:0> ok(4=
=2Cx.size + 3)=3D> nil
Insert mode =
 
R

Robert Klemme

An issue pops up when I use a method's integer return value in a function argument while performing arithmetic on that value. Then missing whitespace between the arithmetical operator and the second argument can cause random errors, but only if there is whitespace preceding the operator. I don't want to rush to judgement and call this a bug, but it certainly seems like one, hidden till now because you have have formatting that's slightly ugly for the error to turn up. Here's an irb session that demonstrates the issue:
irb(main):062:0* x = '123456'=> "123456"irb(main):063:0> x.slice(1, x.size -2)SyntaxError: compile error(irb):63: syntax error, unexpected ')', expecting kDO_BLOCK from (irb):63 from :0irb(main):064:0> x.slice(1, x.size - 2)=> "2345"irb(main):065:0> xslice(1, x.size-2)=> "2345"irb(main):066:0> x.slice(1, x.size -2)SyntaxError: compile error(irb):66: syntax error, unexpected ')', expecting kDO_BLOCK from (irb):66 from :0irb(main):067:0> x[x.size -3,1]ArgumentError: wrong number of arguments (2 for 0) from (irb):67:in `size' from (irb):67 from :0irb(main):068:0> x[x.size - 3,1]=> "4"irb(main):069:0> def ok(a,b)irb(main):070:1> end=> nilirb(main):071:0> ok(x.size +3,4)ArgumentError: wrong number of arguments (2 for 0) from (irb):71:in `size' from (irb):71 from :0irb(main):072:0> ok(4,x.size +3)SyntaxError: compile error(irb):72: syntax error, unexpected ')', expecting kDO_BLOCK from (irb):72 from :0irb(main):074:0> ok(4,x.size + 3)=> nil

Can you repost with proper indentation and line wrapping? Guessing
from your text I assume you might have fallen into a unary / binary
operator ambiguity.

Sorry about that, Hotmail's "rich text" formatting messed it up I believe Here it goes again with plain text:
irb(main):062:0* x = '123456'=> "123456"irb(main):063:0> x.slice(1, xsize -2)SyntaxError: compile error(irb):63: syntax error, unexpected ')', expecting kDO_BLOCK from (irb):63 from :0 irb(main):064:0> x.slice(1, x.size - 2)=> "2345"irb(main):065:0> x.slice(1, x.size-2)=> "2345"irb(main):066:0> x.slice(1, x.size -2)SyntaxError: compile error(irb):66: syntax error, unexpected ')', expecting kDO_BLOCK from (irb):66 from :0 irb(main):067:0> x[x.size -3,1]ArgumentError: wrong number of arguments (2 for 0) from (irb):67:in `size' from (irb):67 from :0 irb(main):068:0> x[x.size - 3,1]=> "4"irb(main):069:0> def ok(a,b)irb(main):070:1> end=> nilirb(main):071:0> ok(x.size +3,4)ArgumentError: wrong number of arguments (2 for 0) from (irb):71:in `size' from (irb):71 from :0 irb(main):072:0> ok(4,x.size +3)SyntaxError: compile error(irb):72: syntax error, unexpected ')', expecting kDO_BLOCK
from (irb):72 from :0 irb(main):074:0> ok(4,x.size + 3)=> nil
Insert mode

Not really... :)

robert
 
E

Ehsanul Hoque

tion argument while performing arithmetic on that value. Then missing white=
space between the arithmetical operator and the second argument can cause r=
andom errors=2C but only if there is whitespace preceding the operator. I d=
on't want to rush to judgement and call this a bug=2C but it certainly seem=
s like one=2C hidden till now because you have have formatting that's sligh=
tly ugly for the error to turn up. Here's an irb session that demonstrates =
the issue:1=2C x.size -2)SyntaxError: compile error(irb):63: syntax error=2C unexpect=
ed ')'=2C expecting kDO_BLOCK from (irb):63 from :0irb(main):064:0> x.slice=
(1=2C x.size - 2)=3D> "2345"irb(main):065:0> xslice(1=2C x.size-2)=3D> "234=
5"irb(main):066:0> x.slice(1=2C x.size -2)SyntaxError: compile error(irb):6=
6: syntax error=2C unexpected ')'=2C expecting kDO_BLOCK from (irb):66 from=
:0irb(main):067:0> x[x.size -3=2C1]ArgumentError: wrong number of argument=
s (2 for 0) from (irb):67:in `size' from (irb):67 from :0irb(main):068:0> x=
[x.size - 3=2C1]=3D> "4"irb(main):069:0> def ok(a=2Cb)irb(main):070:1> end=
=3D> nilirb(main):071:0> ok(x.size +3=2C4)ArgumentError: wrong number of ar=
guments (2 for 0) from (irb):71:in `size' from (irb):71 from :0irb(main):07=
2:0> ok(4=2Cx.size +3)SyntaxError: compile error(irb):72: syntax error=2C u=
nexpected ')'=2C expecting kDO_BLOCK from
(irb):72 from :0irb(main):074:0> ok(4=2Cx.size + 3)=3D> nil
=2C xsize -2)SyntaxError: compile error(irb):63: syntax error=2C unexpected=
')'=2C expecting kDO_BLOCK from (irb):63 from :0 irb(main):064:0> x.slice(=
1=2C x.size - 2)=3D> "2345"irb(main):065:0> x.slice(1=2C x.size-2)=3D> "234=
5"irb(main):066:0> x.slice(1=2C x.size -2)SyntaxError: compile error(irb):6=
6: syntax error=2C unexpected ')'=2C expecting kDO_BLOCK from (irb):66 from=
:0 irb(main):067:0> x[x.size -3=2C1]ArgumentError: wrong number of argumen=
ts (2 for 0) from (irb):67:in `size' from (irb):67 from :0 irb(main):068:0>=
x[x.size - 3=2C1]=3D> "4"irb(main):069:0> def ok(a=2Cb)irb(main):070:1> en=
d=3D> nilirb(main):071:0> ok(x.size +3=2C4)ArgumentError: wrong number of a=
rguments (2 for 0) from (irb):71:in `size' from (irb):71 from :0 irb(main):=
072:0> ok(4=2Cx.size +3)SyntaxError: compile error(irb):72: syntax error=2C=
unexpected ')'=2C expecting kDO_BLOCK
from (irb):72 from :0 irb(main):074:0> ok(4=2Cx.size + 3)=3D> nil

Not really... :)

robert
You know what=2C I should probably just use pastie. Here you go:=A0http://p=
astie.org/1267606



Insert mode =
 
J

Justin Collins

Ehsanul said:
An issue pops up when I use a method's integer return value in a function argument while performing arithmetic on that value. Then missing whitespace between the arithmetical operator and the second argument can cause random errors, but only if there is whitespace preceding the operator. I don't want to rush to judgement and call this a bug, but it certainly seems like one, hidden till now because you have have formatting that's slightly ugly for the error to turn up. Here's an irb session that demonstrates the issue:
irb(main):062:0* x = '123456'=> "123456"irb(main):063:0> x.slice(1, x.size -2)SyntaxError: compile error(irb):63: syntax error, unexpected ')', expecting kDO_BLOCK from (irb):63 from :0irb(main):064:0> x.slice(1, x.size - 2)=> "2345"irb(main):065:0> xslice(1, x.size-2)=> "2345"irb(main):066:0> x.slice(1, x.size -2)SyntaxError: compile error(irb):66: syntax error, unexpected ')', expecting kDO_BLOCK from (irb):66 from :0irb(main):067:0> x[x.size -3,1]ArgumentError: wrong number of arguments (2 for 0) from (irb):67:in `size' from (irb):67 from :0irb(main):068:0> x[x.size - 3,1]=> "4"irb(main):069:0> def ok(a,b)irb(main):070:1> end=> nilirb(main):071:0> ok(x.size +3,4)ArgumentError: wrong number of arguments (2 for 0) from (irb):71:in `size' from (irb):71 from :0irb(main):072:0> ok(4,x.size +3)SyntaxError: compile error(irb):72: syntax error, unexpected ')', expecting kDO_BLOCK from
(irb):72 from :0irb(main):074:0> ok(4,x.size + 3)=> nil
Can you repost with proper indentation and line wrapping? Guessing
from your text I assume you might have fallen into a unary / binary
operator ambiguity.

Sorry about that, Hotmail's "rich text" formatting messed it up I believe Here it goes again with plain text:
irb(main):062:0* x = '123456'=> "123456"irb(main):063:0> x.slice(1, xsize -2)SyntaxError: compile error(irb):63: syntax error, unexpected ')', expecting kDO_BLOCK from (irb):63 from :0 irb(main):064:0> x.slice(1, x.size - 2)=> "2345"irb(main):065:0> x.slice(1, x.size-2)=> "2345"irb(main):066:0> x.slice(1, x.size -2)SyntaxError: compile error(irb):66: syntax error, unexpected ')', expecting kDO_BLOCK from (irb):66 from :0 irb(main):067:0> x[x.size -3,1]ArgumentError: wrong number of arguments (2 for 0) from (irb):67:in `size' from (irb):67 from :0 irb(main):068:0> x[x.size - 3,1]=> "4"irb(main):069:0> def ok(a,b)irb(main):070:1> end=> nilirb(main):071:0> ok(x.size +3,4)ArgumentError: wrong number of arguments (2 for 0) from (irb):71:in `size' from (irb):71 from :0 irb(main):072:0> ok(4,x.size +3)SyntaxError: compile error(irb):72: syntax error, unexpected ')', expecting kDO_BLOCK
from (irb):72 from :0 irb(main):074:0> ok(4,x.size + 3)=> nil
Insert mode
Not really... :)

robert
You know what, I should probably just use pastie. Here you go: http://pastie.org/1267606



Insert mode

Robert was right.

ok(x.size +3,4)


is parsed as


ok(x.size(+3, 4))


while you seem to be expecting


ok((x.size + 3), 4))


Similarly with all your other examples.

-Justin
 
E

Ehsanul Hoque

----------------------------------------
Date: Wed=2C 3 Nov 2010 11:35:03 +0900
From: (e-mail address removed)
Subject: Re: Strange whitespace parsing behavior on Ruby 1.8.7 (patchleve= l 249/302)
To: (e-mail address removed)
nction argument while performing arithmetic on that value. Then missing whi=
tespace between the arithmetical operator and the second argument can cause=
random errors=2C but only if there is whitespace preceding the operator. I=
don't want to rush to judgement and call this a bug=2C but it certainly se=
ems like one=2C hidden till now because you have have formatting that's sli=
ghtly ugly for the error to turn up. Here's an irb session that demonstrate=
s the issue:e(1=2C x.size -2)SyntaxError: compile error(irb):63: syntax error=2C unexpe=
cted ')'=2C expecting kDO_BLOCK from (irb):63 from :0irb(main):064:0> x.sli=
ce(1=2C x.size - 2)=3D> "2345"irb(main):065:0> xslice(1=2C x.size-2)=3D> "2=
345"irb(main):066:0> x.slice(1=2C x.size -2)SyntaxError: compile error(irb)=
:66: syntax error=2C unexpected ')'=2C expecting kDO_BLOCK from (irb):66 fr=
om :0irb(main):067:0> x[x.size -3=2C1]ArgumentError: wrong number of argume=
nts (2 for 0) from (irb):67:in `size' from (irb):67 from :0irb(main):068:0>=
x[x.size - 3=2C1]=3D> "4"irb(main):069:0> def ok(a=2Cb)irb(main):070:1> en=
d=3D> nilirb(main):071:0> ok(x.size +3=2C4)ArgumentError: wrong number of a=
rguments (2 for 0) from (irb):71:in `size' from (irb):71 from :0irb(main):0=
72:0> ok(4=2Cx.size +3)SyntaxError: compile error(irb):72: syntax error=2C =
unexpected ')'=2C expecting kDO_BLOCK from1=2C xsize -2)SyntaxError: compile error(irb):63: syntax error=2C unexpecte=
d ')'=2C expecting kDO_BLOCK from (irb):63 from :0 irb(main):064:0> x.slice=
(1=2C x.size - 2)=3D> "2345"irb(main):065:0> x.slice(1=2C x.size-2)=3D> "23=
45"irb(main):066:0> x.slice(1=2C x.size -2)SyntaxError: compile error(irb):=
66: syntax error=2C unexpected ')'=2C expecting kDO_BLOCK from (irb):66 fro=
m :0 irb(main):067:0> x[x.size -3=2C1]ArgumentError: wrong number of argume=
nts (2 for 0) from (irb):67:in `size' from (irb):67 from :0 irb(main):068:0=
x[x.size - 3=2C1]=3D> "4"irb(main):069:0> def ok(a=2Cb)irb(main):070:1> e=
nd=3D> nilirb(main):071:0> ok(x.size +3=2C4)ArgumentError: wrong number of =
arguments (2 for 0) from (irb):71:in `size' from (irb):71 from :0 irb(main)=
:072:0> ok(4=2Cx.size +3)SyntaxError: compile error(irb):72: syntax error=
=2C unexpected ')'=2C expecting kDO_BLOCK
Robert was right.

ok(x.size +3=2C4)


is parsed as


ok(x.size(+3=2C 4))


while you seem to be expecting


ok((x.size + 3)=2C 4))


Similarly with all your other examples.

-Justin


That makes sense. But the inconsistency between the different behaviors giv=
en just a small difference in whitespace is still quite off-putting.

Also how is this line parsed?:

x.slice(1=2C x.size -2)=A0

- It seems unreasonable to parse it any way other than that intended.Insert=
mode =
 
J

Justin Collins

function argument while performing arithmetic on that value. Then missing=
whitespace between the arithmetical operator and the second argument can=
cause random errors, but only if there is whitespace preceding the opera=
tor. I don't want to rush to judgement and call this a bug, but it certai=
nly seems like one, hidden till now because you have have formatting that=
's slightly ugly for the error to turn up. Here's an irb session that dem=
onstrates the issue:slice(1, x.size -2)SyntaxError: compile error(irb):63: syntax error, unex=
pected ')', expecting kDO_BLOCK from (irb):63 from :0irb(main):064:0> x.=
slice(1, x.size - 2)=3D> "2345"irb(main):065:0> xslice(1, x.size-2)=3D>=
"2345"irb(main):066:0> x.slice(1, x.size -2)SyntaxError: compile error=
(irb):66: syntax error, unexpected ')', expecting kDO_BLOCK from (irb):66=
from :0irb(main):067:0> x[x.size -3,1]ArgumentError: wrong number of ar=
guments (2 for 0) from (irb):67:in `size' from (irb):67 from :0irb(main):=
068:0> x[x.size - 3,1]=3D> "4"irb(main):069:0> def ok(a,b)irb(main):07=
0:1> end=3D> nilirb(main):071:0> ok(x.size +3,4)ArgumentError: wrong n=
umber of arguments (2 for 0) from (irb):71:in `size' from (irb):71 from :=
0irb(main):072:0> ok(4,x.size +3)SyntaxError: compile error(irb):72: syn=
tax error, unexpected ')', expecting kDO_BLOCK fromice(1, xsize -2)SyntaxError: compile error(irb):63: syntax error, unexpec=
ted ')', expecting kDO_BLOCK from (irb):63 from :0 irb(main):064:0> x.sl=
ice(1, x.size - 2)=3D> "2345"irb(main):065:0> x.slice(1, x.size-2)=3D> =
"2345"irb(main):066:0> x.slice(1, x.size -2)SyntaxError: compile error(=
irb):66: syntax error, unexpected ')', expecting kDO_BLOCK from (irb):66 =
from :0 irb(main):067:0> x[x.size -3,1]ArgumentError: wrong number of ar=
guments (2 for 0) from (irb):67:in `size' from (irb):67 from :0 irb(main)=
:068:0> x[x.size - 3,1]=3D> "4"irb(main):069:0> def ok(a,b)irb(main):0=
70:1> end=3D> nilirb(main):071:0> ok(x.size +3,4)ArgumentError: wrong =
number of arguments (2 for 0) from (irb):71:in `size' from (irb):71 from =
:0 irb(main):072:0> ok(4,x.size +3)SyntaxError: compile error(irb):72: s=
yntax error, unexpected ')', expecting kDO_BLOCK
That makes sense. But the inconsistency between the different behaviors=
given just a small difference in whitespace is still quite off-putting.
Also how is this line parsed?:

x.slice(1, x.size -2)=20

- It seems unreasonable to parse it any way other than that intended.In= sert mode =09
=20

Poorly.

Also, I somehow missed Colin and Robert's answers, so I'll refer you to=20
them.

-Justin
 
P

Phillip Gawlowski

That makes sense. But the inconsistency between the different behaviors given just a small difference in whitespace is still quite off-putting.

One whitespace or an infinite number is rather irrelevant. A small
difference to you is a world of difference for a computer. ;)
Also how is this line parsed?:

x.slice(1, x.size -2)

- It seems unreasonable to parse it any way other than that intended.

Computers can't read your mind. They do what you tell them, not what
you mean. That results in you having to do the work of expressing your
thoughts in terms the computer can actually understand and that are
also logically correct.

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.
 
C

Colin Bartlett

[Note: parts of this message were removed to make it a legal post.]

That makes sense. But the inconsistency between the different behaviors
given just a small difference in whitespace is still quite off-putting.

Also how is this line parsed?:

x.slice(1, x.size -2)

- It seems unreasonable to parse it any way other than that intended.Insert
mode

Subject to correction by Robert - or anyone else! - to summarise I think the
basic problem for Ruby here is when to parse "-2" (without any space between
the "-" and the "2") as a unary operator on 2 and as a binary operator on a
previous "token" with 2 as the argument.

At one extreme, Ruby could raise an error if there is any possible
ambiguity. Ruby, I think, tries to do the best it can with *limited*
knowledge of the methods being interpreted.

For example:
7 -2 => 5
this works for a similar reason to the reason this works:
x = 13
x -2 #=> 11
Ruby knows (or anyway interprets) x as a variable, not a method, so no
ambiguity: the "-" is either a mistake or is a binary operator.

x.size #=> 4
x.size-3 #=> 1
I'm guessing that here Ruby "thinks" no whitespace, so bind the "-" to the
first possible token, which is (x.size), so "-" gets treated as a binary
operator.

But in:
x.size -2
this could mean
((x.size) - 2) or (x.size( -2 ))
In this case size doesn't take any arguments, so with some more work the
interpreter could parse it as being likely to be ((x.size)-2), but that
won't work for methods which can take zero or one (or more) arguments.
So (again a guess) the idea is that the interpreter won't even try to to
some extra work which might, or might not, resolve the ambiguity.

The result is what seems to me to be a reasonable compromise, but I ought to
add that some other possibilities might also be a reasonable (but different)
compromise!
 
R

Robert Klemme

Subject to correction by Robert - or anyone else! - to summarise I think = the
basic problem for Ruby here is when to parse "-2" (without any space betw= een
the "-" and the "2") as a unary operator on 2 and as a binary operator on= a
previous "token" with 2 as the argument.

At one extreme, Ruby could raise an error if there is any possible
ambiguity. Ruby, I think, tries to do the best it can with *limited*
knowledge of the methods being interpreted.

For example:
=A07 -2 =3D> 5
this works for a similar reason to the reason this works:
=A0x =3D 13
=A0x -2 #=3D> 11
Ruby knows (or anyway interprets) x as a variable, not a method, so no
ambiguity: the "-" is either a mistake or is a binary operator.

I think the Ruby interpreter _knows_ it because during parsing it
recognizes "x =3D 13" as a local variable assignment which makes x a
local variable and will shadow method x (if it exists). That
knowledge might also be used to disambiguate "x -2" which does not
really make sens with a local variable if not interpreted as an
expression with a binary "-".
=A0x.size #=3D> 4
=A0x.size-3 #=3D> 1
I'm guessing that here Ruby "thinks" no whitespace, so bind the "-" to th= e
first possible token, which is (x.size), so "-" gets treated as a binary
operator.

But in:
=A0x.size -2
this could mean
((x.size) - 2) or (x.size( -2 ))
In this case size doesn't take any arguments, so with some more work the
interpreter could parse it as being likely to be ((x.size)-2), but that
won't work for methods which can take zero or one (or more) arguments.
So (again a guess) the idea is that the interpreter won't even try to to
some extra work which might, or might not, resolve the ambiguity.

The result is what seems to me to be a reasonable compromise, but I ought= to
add that some other possibilities might also be a reasonable (but differe= nt)
compromise!

Absolutely agree. I'd also like to add that this has never been an
issue for me in practice. So we are probably putting something under
a microscope which is actually really small. :)

Cheers

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top