[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Very weird behaviour of Ruby 1.8.6 with multi-line parenthesis expressions

nicolas.lehuen

8/6/2007 10:21:00 PM

Hi,

I've just lost a few hours on this strange behaviour (bug ?).
Apparently it is caused by some kind of operator precedence
thingamagic. My original code was of course much more complicated, so
finding the problem wasn't easy (I first thought my code was
buggy...). Here is a piece of minimal code that reproduces the
problem :

8<-------------8<-------------8<-------------8<-------------

C:\temp>ver

Microsoft Windows [version 6.0.6000]

C:\temp>ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

C:\temp>type bug.rb
sum1 = 1 + 1 + 1

puts sum1

sum2 = (
1
+ 1
+ 1
)

puts sum2

C:\temp>ruby bug.rb
3
1

8<-------------8<-------------8<-------------8<-------------

>From now on I promise I'll remember that you cannot safely use multi-
line parenthesis expressions in Ruby ; I just would like to understand
what this code means to Ruby, if it's not "give me the result of 1 + 1
+ 1".

The same takes place in irb :

C:\temp>irb
irb(main):001:0> sum1 = 1 + 1 + 1
=> 3
irb(main):002:0> sum2 = (
irb(main):003:1* 1
irb(main):004:1> + 1
irb(main):005:1> + 1
irb(main):006:1> )
=> 1
irb(main):007:0> op3 = (
irb(main):008:1* 1
irb(main):009:1> * 3
irb(main):010:1> * 9
irb(main):011:1> )
SyntaxError: compile error
(irb):9: syntax error, unexpected '\n', expecting tCOLON2 or '[' or
'.'
from (irb):11
from :0

Duh ! This must mean something, but what ?

Regards,

Nicolas Lehuen

7 Answers

Gregory Brown

8/6/2007 10:35:00 PM

0

On 8/6/07, Nicolas Lehuen <nicolas.lehuen@gmail.com> wrote:
> Hi,
>
> I've just lost a few hours on this strange behaviour (bug ?).
> Apparently it is caused by some kind of operator precedence
> thingamagic. My original code was of course much more complicated, so
> finding the problem wasn't easy (I first thought my code was
> buggy...). Here is a piece of minimal code that reproduces the
> problem :
>
> 8<-------------8<-------------8<-------------8<-------------
>
> C:\temp>ver
>
> Microsoft Windows [version 6.0.6000]
>
> C:\temp>ruby -v
> ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
>
> C:\temp>type bug.rb
> sum1 = 1 + 1 + 1
>
> puts sum1
>
> sum2 = (
> 1
> + 1
> + 1

+ is a unary operator.

this parses as:

(
1; +1; +1;
)

If you want 3, do:

1 +
1 +
1

which parses as 1 + 1 + 1

Tom Werner

8/6/2007 10:40:00 PM

0

Nicolas Lehuen wrote:
> Hi,
>
> I've just lost a few hours on this strange behaviour (bug ?).
> Apparently it is caused by some kind of operator precedence
> thingamagic. My original code was of course much more complicated, so
> finding the problem wasn't easy (I first thought my code was
> buggy...). Here is a piece of minimal code that reproduces the
> problem :
>
> 8<-------------8<-------------8<-------------8<-------------
>
> C:\temp>ver
>
> Microsoft Windows [version 6.0.6000]
>
> C:\temp>ruby -v
> ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
>
> C:\temp>type bug.rb
> sum1 = 1 + 1 + 1
>
> puts sum1
>
> sum2 = (
> 1
> + 1
> + 1
> )
>
> puts sum2
>
> C:\temp>ruby bug.rb
> 3
> 1
>
> 8<-------------8<-------------8<-------------8<-------------
>
> >From now on I promise I'll remember that you cannot safely use multi-
> line parenthesis expressions in Ruby ; I just would like to understand
> what this code means to Ruby, if it's not "give me the result of 1 + 1
> + 1".
>
> The same takes place in irb :
>
> C:\temp>irb
> irb(main):001:0> sum1 = 1 + 1 + 1
> => 3
> irb(main):002:0> sum2 = (
> irb(main):003:1* 1
> irb(main):004:1> + 1
> irb(main):005:1> + 1
> irb(main):006:1> )
> => 1
> irb(main):007:0> op3 = (
> irb(main):008:1* 1
> irb(main):009:1> * 3
> irb(main):010:1> * 9
> irb(main):011:1> )
> SyntaxError: compile error
> (irb):9: syntax error, unexpected '\n', expecting tCOLON2 or '[' or
> '.'
> from (irb):11
> from :0
>
> Duh ! This must mean something, but what ?
>
> Regards,
>
> Nicolas Lehuen
>
>
>
>
+1 is a valid ruby statement by itself. In order for the interpreter to
realize that your statement continues onto another line, you must end
the line with an operator that begs for more. In your case, moving the
pluses up to the previous lines will work:

irb(main):003:0> (1 +
irb(main):004:1* 1 +
irb(main):005:1* 1)
=> 3

You get that error in irb with multiplication because * 3 is not a valid
ruby statement by itself (remember that + 1 is valid, meaning simply, 1).

Hope this helps!

Tom

nicolas.lehuen

8/6/2007 10:49:00 PM

0

OK, now I get it, pluses need to go at the end of the line. Thanks
Gregory and Tom !

Regards,
Nicolas

Michael Hollins

8/6/2007 11:38:00 PM

0

Tom Werner wrote:
> Nicolas Lehuen wrote:
>> Hi,
>>
>> I've just lost a few hours on this strange behaviour (bug ?).
>> Apparently it is caused by some kind of operator precedence
>> thingamagic. My original code was of course much more complicated, so
>> finding the problem wasn't easy (I first thought my code was
>> buggy...). Here is a piece of minimal code that reproduces the
>> problem :
>>
>> 8<-------------8<-------------8<-------------8<-------------
>>
>> C:\temp>ver
>>
>> Microsoft Windows [version 6.0.6000]
>>
>> C:\temp>ruby -v
>> ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
>>
>> C:\temp>type bug.rb
>> sum1 = 1 + 1 + 1
>>
>> puts sum1
>>
>> sum2 = (
>> 1
>> + 1
>> + 1
>> )
>>
>> puts sum2
>>
>> C:\temp>ruby bug.rb
>> 3
>> 1
>>
>> 8<-------------8<-------------8<-------------8<-------------
>>
>> >From now on I promise I'll remember that you cannot safely use multi-
>> line parenthesis expressions in Ruby ; I just would like to understand
>> what this code means to Ruby, if it's not "give me the result of 1 + 1
>> + 1".
>>
>> The same takes place in irb :
>>
>> C:\temp>irb
>> irb(main):001:0> sum1 = 1 + 1 + 1
>> => 3
>> irb(main):002:0> sum2 = (
>> irb(main):003:1* 1
>> irb(main):004:1> + 1
>> irb(main):005:1> + 1
>> irb(main):006:1> )
>> => 1
>> irb(main):007:0> op3 = (
>> irb(main):008:1* 1
>> irb(main):009:1> * 3
>> irb(main):010:1> * 9
>> irb(main):011:1> )
>> SyntaxError: compile error
>> (irb):9: syntax error, unexpected '\n', expecting tCOLON2 or '[' or
>> '.'
>> from (irb):11
>> from :0
>>
>> Duh ! This must mean something, but what ?
>>
>> Regards,
>>
>> Nicolas Lehuen
>>
>>
>>
>>
> +1 is a valid ruby statement by itself. In order for the interpreter to
> realize that your statement continues onto another line, you must end
> the line with an operator that begs for more. In your case, moving the
> pluses up to the previous lines will work:
>
> irb(main):003:0> (1 +
> irb(main):004:1* 1 +
> irb(main):005:1* 1)
> => 3
>
> You get that error in irb with multiplication because * 3 is not a valid
> ruby statement by itself (remember that + 1 is valid, meaning simply, 1).
>

Are there any interesting uses of

+ x

as a statement on its own, or is it likely that
it's a programmer error 99 times out 100? If the latter, perhaps it's worth
the ruby interpreter emitting a warning when it sees such constructs, much
like the warning you get when you don't use parantheses around argument lists
in certain situations.

I'm happy to be told that there are valid uses of "+ x". I'm still learning something
new about ruby every day.

cheers,
mick

Tom Werner

8/6/2007 11:55:00 PM

0

Michael Hollins wrote:
>
> Are there any interesting uses of
>
> + x
>
> as a statement on its own, or is it likely that
> it's a programmer error 99 times out 100? If the latter, perhaps it's
> worth
> the ruby interpreter emitting a warning when it sees such constructs,
> much
> like the warning you get when you don't use parantheses around
> argument lists
> in certain situations.
>
> I'm happy to be told that there are valid uses of "+ x". I'm still
> learning something
> new about ruby every day.
>
> cheers,
> mick
>
>
I've seen it used in DSLs (domain specific languages) built with ruby.
It can be defined on your own classes like so:

class Foo
def initialize(x)
@x = x
end

def +@
@x.reverse
end
end

+Foo.new('foo')
# => "oof"

I don't think I've ever used it in any of my code, but that doesn't mean
there aren't legitimate uses for it. Anyone else used unary plus for
something useful?

Tom

Joel VanderWerf

8/7/2007 12:37:00 AM

0

Michael Hollins wrote:
> Are there any interesting uses of
>
> + x
>
> as a statement on its own, or is it likely that
> it's a programmer error 99 times out 100?

Not that I'm recommending it, but just as an idea:


module Enumerable
def +@
inject {|s,x| s+(+x)}
end
end

class Object
def +x
x
end
end

p +[1,2,3] # => 6

sum = +[1, [2, 3], [4.0]]

p sum # => 10.0

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Pit Capitain

8/7/2007 8:02:00 PM

0

2007/8/7, Michael Hollins <mick@hollins.id.au>:
> Are there any interesting uses of
>
> + x
>
> as a statement on its own, or is it likely that
> it's a programmer error 99 times out 100?

I sometimes use +x side by side with -x. For example

UP = -1
DOWN = +1

Regards,
Pit