# Bug: multiline ruby expressions with parens

**URL:** <https://www.ruby-forum.com/t/bug-multiline-ruby-expressions-with-parens/135297>\
**Category:** Ruby\
**Created:** [April 20, 2008, 10:25pm UTC](https://www.ruby-forum.com/t/bug-multiline-ruby-expressions-with-parens/135297 "2008-04-20T22:25:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![asoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@asoto](https://www.ruby-forum.com/u/asoto)\
**Post date:** [April 20, 2008, 10:25pm UTC](https://www.ruby-forum.com/t/bug-multiline-ruby-expressions-with-parens/135297/1 "2008-04-20T22:25:26Z")

</div>

Hi,

Anyone aware of this bug?

$ cat expr-bug.rb  
a = (2

- 
  1. / 2  
p a

a = (2 \

- 
  1. / 2  
p a  
$ ruby expr-bug.rb  
1  
2  
$ ruby --version  
ruby 1.8.6 (2008-03-03 patchlevel 114) [x86\_64-linux]

---

<div class="post-metadata">

**Author:** ![asoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@asoto](https://www.ruby-forum.com/u/asoto)\
**Post date:** [April 20, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/bug-multiline-ruby-expressions-with-parens/135297/2 "2008-04-20T23:40:07Z")

</div>

Alex S. wrote:

> Hi,
> 
> Anyone aware of this bug?
> 
> $ cat expr-bug.rb  
> a = (2
> 
> - 
> 1. / 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.)

---

<div class="post-metadata">

**Author:** ![asoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@asoto](https://www.ruby-forum.com/u/asoto)\
**Post date:** [April 21, 2008, 10:35am UTC](https://www.ruby-forum.com/t/bug-multiline-ruby-expressions-with-parens/135297/3 "2008-04-21T10:35:38Z")

</div>

On 2008-04-20, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> AFAIK not a bug. It’s because parens can contain two or more  
> p a # ==\> 4
> 
> (I’m not advocating either of the above forms, FWIW.)

I was wondering what that had to do with:

a = (2

- 
  1. / 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?

---

<div class="post-metadata">

**Author:** ![asoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@asoto](https://www.ruby-forum.com/u/asoto)\
**Post date:** [April 21, 2008, 7:07pm UTC](https://www.ruby-forum.com/t/bug-multiline-ruby-expressions-with-parens/135297/4 "2008-04-21T19:07:00Z")

</div>

Jim C. wrote:

> > AFAIK not a bug. It’s because parens can contain two or more  
> > p a # ==\> 4  
> > implying that the above is equivalent to:
> 
> 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.

---

<div class="post-metadata">

**Author:** ![asoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@asoto](https://www.ruby-forum.com/u/asoto)\
**Post date:** [April 22, 2008, 8:05am UTC](https://www.ruby-forum.com/t/bug-multiline-ruby-expressions-with-parens/135297/5 "2008-04-22T08:05:43Z")

</div>

On Apr 21, 11:39 am, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > - 
> > 1. / 2
> 
> 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. 😉

---

<div class="post-metadata">

**Author:** ![asoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58956e/32.png) [@asoto](https://www.ruby-forum.com/u/asoto)\
**Post date:** [April 21, 2008, 10:41am UTC](https://www.ruby-forum.com/t/bug-multiline-ruby-expressions-with-parens/135297/6 "2008-04-21T10:41:03Z")

</div>

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

> - 
> 1. / 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.
