[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Bug: multiline ruby expressions with parens

Alex Shulgin

4/20/2008 8:23:00 PM

Hi,

Anyone aware of this bug?

$ cat expr-bug.rb
a = (2
+ 2) / 2
p a

a = (2 + 2) / 2
p a
$ ruby expr-bug.rb
1
2
$ ruby --version
ruby 1.8.6 (2008-03-03 patchlevel 114) [x86_64-linux]

--
Alex
6 Answers

Phlip

4/20/2008 8:33:00 PM

0

Alex Shulgin wrote:

> Anyone aware of this bug?
>
> $ cat expr-bug.rb
> a = (2
> + 2) / 2
> p a

It's a feature. The exact parsing rule seems to be "trailing binary
operators can bind to the next item". So (2 + \n 2) behaves as you
expect, but (2 \n + 2) interprets as (2; +2). Then ; returns its last
operand, which is "unary positive 2".

I'm not sure what use a unitary positive is, except overloaded operator
fodder...

--
Phlip

Joel VanderWerf

4/20/2008 9:39:00 PM

0

Alex Shulgin wrote:
> Hi,
>
> Anyone aware of this bug?
>
> $ cat expr-bug.rb
> a = (2
> + 2) / 2
> p a

AFAIK not a bug. It's because parens can contain two or more
expressions, separated by either newlines or semicolons.

x = 5

a = (x+=1
x + 2) / 2
p a # ==> 4

#equiv to:
a = (x+=1; x + 2) / 2
p a # ==> 4

(I'm not advocating either of the above forms, FWIW.)

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

Jim Cochrane

4/21/2008 8:32:00 AM

0

On 2008-04-20, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
> Alex Shulgin wrote:
>> Hi,
>>
>> Anyone aware of this bug?
>>
>> $ cat expr-bug.rb
>> a = (2
>> + 2) / 2
>> p a
>
> AFAIK not a bug. It's because parens can contain two or more
> expressions, separated by either newlines or semicolons.
>
> x = 5
>
> a = (x+=1
> x + 2) / 2
> p a # ==> 4
>
> #equiv to:
> a = (x+=1; x + 2) / 2
> p a # ==> 4
>
> (I'm not advocating either of the above forms, FWIW.)
>

I was wondering what that had to do with:

a = (2
+ 2) / 2

until I realized (I think), after experimenting with irb that you are
implying that the above is equivalent to:

a = (2;
+2) / 2

which is the same as 'a = (+2) / 2' -> 'a = 2/2' -> a = 1

but:

a = (2 +
2) / 2

is the same as 'a = (2 + 2) / 2' -> 'a = 4 / 2'


Is my understanding correct?

--

Eric Hodel

4/21/2008 8:40:00 AM

0

On Apr 20, 2008, at 13:25 PM, Alex Shulgin wrote:

> Hi,
>
> Anyone aware of this bug?
>
> $ cat expr-bug.rb
> a = (2
> + 2) / 2
> p a
>
> a = (2 > + 2) / 2
> p a
> $ ruby expr-bug.rb
> 1
> 2
> $ ruby --version
> ruby 1.8.6 (2008-03-03 patchlevel 114) [x86_64-linux]

This is not a bug. Newlines are significant in ruby.

Joel VanderWerf

4/21/2008 5:06:00 PM

0

Jim Cochrane wrote:
> On 2008-04-20, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
>> Alex Shulgin wrote:
>>> Hi,
>>>
>>> Anyone aware of this bug?
>>>
>>> $ cat expr-bug.rb
>>> a = (2
>>> + 2) / 2
>>> p a
>> AFAIK not a bug. It's because parens can contain two or more
>> expressions, separated by either newlines or semicolons.
>>
>> x = 5
>>
>> a = (x+=1
>> x + 2) / 2
>> p a # ==> 4
>>
>> #equiv to:
>> a = (x+=1; x + 2) / 2
>> p a # ==> 4
>>
>> (I'm not advocating either of the above forms, FWIW.)
>>
>
> I was wondering what that had to do with:
>
> a = (2
> + 2) / 2
>
> until I realized (I think), after experimenting with irb that you are
> implying that the above is equivalent to:
>
> a = (2;
> +2) / 2
>
> which is the same as 'a = (+2) / 2' -> 'a = 2/2' -> a = 1
>
> but:
>
> a = (2 +
> 2) / 2
>
> is the same as 'a = (2 + 2) / 2' -> 'a = 4 / 2'

Right. I should have said that the expression

+ 2

is treated as applying the unary operator #+ to the integer 2.

Be careful with irb... sometimes it's a little different from ruby.

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

Alex Shulgin

4/22/2008 6:05:00 AM

0

On Apr 21, 11:39 am, Eric Hodel <drbr...@segment7.net> wrote:
> On Apr 20, 2008, at 13:25 PM, Alex Shulgin wrote:
>
>
>
> > Hi,
>
> > Anyone aware of this bug?
>
> > $ cat expr-bug.rb
> > a = (2
> > + 2) / 2
> > p a
>
> > a = (2 > > + 2) / 2
> > p a
> > $ ruby expr-bug.rb
> > 1
> > 2
> > $ ruby --version
> > ruby 1.8.6 (2008-03-03 patchlevel 114) [x86_64-linux]
>
> This is not a bug. Newlines are significant in ruby.

Yes, I realize now.

But I'd better expect a syntax error like `expecting ')', but found
newline'. Instead of that, the code runs but produces quite
unexpected results and the problem isn't easy to spot if you are not
familiar with this issue.

I think I can get used to break lines after the operator or use a
backslash... My problem is that I've already used to break lines
_before_ operator in C. ;-)

--
Thank you all,
Alex