# Ruby Whitespace Semantics

**URL:** <https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234>\
**Category:** Ruby\
**Created:** [February 27, 2006, 8:45am UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234 "2006-02-27T08:45:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 8:45am UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/1 "2006-02-27T08:45:58Z")

</div>

Can someone please explain the semantics behind the following:

irb(main):001:0\> a = ( 4 + 5 )  
=\> 9  
irb(main):002:0\> a = ( 4  
irb(main):003:1\> + 5 )  
=\> 5  
irb(main):004:0\> a = ( 4 +  
irb(main):005:1\* 5 )  
=\> 9

The first and last statements make sense to me, but why is the second  
one  
returning 5?

I find semantics like this troubling, and no documentation sheds light  
as  
to what would cause this behavior.

Thanks,  
Almann

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 8:55am UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/2 "2006-02-27T08:55:05Z")

</div>

Almann G. wrote:

> The first and last statements make sense to me, but why is the second one  
> returning 5?
> 
> I find semantics like this troubling, and no documentation sheds light as  
> to what would cause this behavior.

I understand your concern. Let me try to clarify.

Expressions in Ruby can be like standalone statements. Statements are  
terminated with an optional semicolon or with a newline. If a statement  
is incomplete, it is understood to go on to the next line; if it is  
complete, it is just as if terminated with a semicolon.

Therefore:

a = (4  
+5)

is the same as

a = (4;  
+5)

or even

a = (4; +5)

That is, it evaluates a “4” and then evaluates a “+5” (which then is the  
resultant value, as it was the last evaluated).

But with

a = (4+  
5)

the parser is able to see that the expression is not complete, and is  
apparently continued on the next line.

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 8:55am UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/3 "2006-02-27T08:55:06Z")

</div>

Almann G. wrote:

> 

I’m no expert, but I think it has to do with both first and third having  
the + operator on the first line and the second one having the +  
operator on the second line - note also that irb understands that the  
third case is a continuation of the previous line (_), but the second  
case is treated almost as 2 separate statements - no (_).

I’m sure someone else will have a better idea  
Kev

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 10:26am UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/4 "2006-02-27T10:26:16Z")

</div>

> =\> 9
> 
> The first and last statements make sense to me, but why is the second one  
> returning 5?
> 
> I find semantics like this troubling, and no documentation sheds light as  
> to what would cause this behavior.  
> This behaviour is documented, at least here:  
> [http://www.rubycentral.com/book/language.html](http://www.rubycentral.com/book/language.html)

and probably in other placess too (but I wouldn’t know about them as I  
am  
learning Ruby for just a little more than 1 week).

Hope it helps,  
Alex

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 9:19am UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/5 "2006-02-27T09:19:25Z")

</div>

Thanks, the semantic is clearer now even though I think it is very  
clumsy.

As an aside, it is interesting that Ruby allows for suites of  
statements to be used in a grouping context (the parenthesis)–most  
other languages that are whitespace sensitive don’t allow this since it  
gets confusing with expressions (as my example shows).

-Almann

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/6 "2006-02-27T17:42:24Z")

</div>

Quoting Almann G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> The first and last statements make sense to me, but why is the  
> second one returning 5?

a = ( 4 + 5 )  
a = ( 4 ; + 5 )  
a = ( 4 + 5 )

A line break begins a new statement unless there is a dangling  
binary operator or line continuation.

-mental

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 4:25pm UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/7 "2006-02-27T16:25:41Z")

</div>

This behavior actually isn’t well documented, since the semantic is  
unclear in that documentation (or the Ruby Manual) what will happen in  
the case that the grouping operator is used in this manner.

In a nutshell, newline delimits statements except when it doesn’t  
(trail with a binary operator for instance)… not a really good  
semantic but I’ll add that to the list of gotchas I need to deal with  
in Ruby.

The 1.4 English Ruby Manual says:  
Each expression are delimited by semicolons(😉 or newlines.

The current Japanese Ruby Manual essentially says the same thing (my  
Japanese isn’t as sharp as it used to be):  
å¼ã¨å¼ã®é??ã¯ã?»ã??ã?³ã?­ã?³(;)ã¾ã?ã¯æ?¹è¡?ã§å?ºå??ã??ã¾ã?  
(shiki to shiki no aida wa semikoron(😉 mata wa kaigyou de kugirimasu)

-Almann

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 6:16pm UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/8 "2006-02-27T18:16:24Z")

</div>

It is generally good coding style in any programming language, when  
continuing an expression across more than one line, to split the lines  
after  
an operator or other punctuation symbol which indicates that the  
expression  
is unfinished. I.e.

```
somefunction( ...very long argument list,
                     more arguments)

```

or

(4 +  
5)

This gives the reader a visual hint that the expression is incomplete,  
and  
continues onto the next line. Ruby just uses the same heuristic that a  
human reader would use.

~Avdi

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 8:04pm UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/9 "2006-02-27T20:04:15Z")

</div>

On 2/27/06, Mark W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > is the same as
> > 
> > a = (4;  
> > +5)
> 
> But (4 is clearly an incomplete statement.

Actually, notice the semicolon. What Hal is demonstrating is that a  
parenthetical group can contain multiple expressions. The newline  
terminates the expression ‘4’, which is a complete expression, while  
the statement (which happens to include that expression) is continued  
on the next line.

Jacob F.

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/10 "2006-02-27T20:01:12Z")

</div>

“Hal F.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> is the same as
> 
> a = (4;  
> +5)

But (4 is clearly an incomplete statement.

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 8:50pm UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/11 "2006-02-27T20:50:33Z")

</div>

You’re not alone. I’ve adopted this practice in C programs ever since I  
read about it in a book called _Human Factors and Typography for More  
Readable Programs_, by Ronald M. Baeker and Aaron Markus, ACM Press,  
1990. The authors recommend breaking long lines at operators “of  
relatively low precedence” and placing the operator at the beginning of  
the second line because “it emphasizes at the beginning of the  
continuation that the second line is a continuation.”

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/12 "2006-02-27T20:56:50Z")

</div>

Quoting Anthony DeRobertis [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Well, when I work in languages other than Ruby (not in ruby, of  
> course, because its a syntax error), I’ve always found:
> 
> foo = a + b + c …  
> + z
> 
> more readable myself, but I’m probably just a weirdo.

It works fine in Ruby if you use a backslash to continue the  
statement:

foo = a + b + c   
+ z

-mental

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 10:50pm UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/13 "2006-02-27T22:50:02Z")

</div>

Ah. I thought a semicolon terminated a statement.

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/14 "2006-02-27T20:22:46Z")

</div>

Avdi G. wrote:

> (4 +  
> 5)
> 
> This gives the reader a visual hint that the expression is incomplete,  
> and  
> continues onto the next line. Ruby just uses the same heuristic that  
> a human reader would use.

Well, when I work in languages other than Ruby (not in ruby, of course,  
because its a syntax error), I’ve always found:

foo = a + b + c …  
+ z

more readable myself, but I’m probably just a weirdo.

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/15 "2006-02-27T23:14:33Z")

</div>

It does. 4 is a valid statement in Ruby. Ruby doesn’t have the same  
sharp division as many other languages. 4 is a statement, 4 + 5 is a  
statement, and +5 is a statement. The return value of a series of  
statements is the value of the final statement. As a result, the value  
of ‘4; +5’ is 5.

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/16 "2006-02-27T23:02:17Z")

</div>

Quoting removed\_email\_address@domain.invalid:

> Ah. I thought a semicolon terminated a statement.

It does; it’s just that the current Ruby grammar permits multiple  
statements within the same set of parenthesis (separated by  
semicolons or newlines).

-mental

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 11:33pm UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/17 "2006-02-27T23:33:20Z")

</div>

On Tue, Feb 28, 2006 at 04:21:02AM +0900, Anthony DeRobertis wrote:

> (…)  
> Well, when I work in languages other than Ruby (not in ruby, of course,  
> because its a syntax error), I’ve always found:
> 
> foo = a + b + c …  
> + z

If this is M(atlab) then the three dots serve to signal  
that the line is continued, i.e. serving the same purpose  
as does \<nl\> in ruby. I.e. you’re explicitely stating already  
that the expression continues on next line (just as you would  
by adding a \<nl\> or ).

-m

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 27, 2006, 11:45pm UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/18 "2006-02-27T23:45:37Z")

</div>

Quoting Timothy G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> It does. 4 is a valid statement in Ruby. Ruby doesn’t have the  
> same sharp division as many other languages.

Well… not really. There’s the same distinction in Ruby too (see  
expr versus compstmt in parse.y).

It’s more that Ruby’s grammar permits statement lists in places that  
many other languages only permit expressions (like inbetween  
parenthesis).

-mental

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 28, 2006, 4:20pm UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/19 "2006-02-28T16:20:43Z")

</div>

Martin S. Weber wrote:

> On Tue, Feb 28, 2006 at 04:21:02AM +0900, Anthony DeRobertis wrote:

> > foo = a + b + c …  
> > + z
> 
> If this is M(atlab) then the three dots serve to signal  
> that the line is continued, i.e. serving the same purpose  
> as does \<nl\> in ruby.

No, that’s pseudo-code and the three dots are an ellipsis indicating  
elided material. Yeah, sure, it should have been U+2026 (â?¦) â?? but I’m  
lazy.

---

<div class="post-metadata">

**Author:** ![Almann\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@Almann\_G](https://www.ruby-forum.com/u/Almann_G)\
**Post date:** [February 28, 2006, 5:07pm UTC](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234/20 "2006-02-28T17:07:10Z")

</div>

On Wed, Mar 01, 2006 at 12:18:20AM +0900, Anthony DeRobertis wrote:

> No, that’s pseudo-code and the three dots are an ellipsis indicating  
> elided material. Yeah, sure, it should have been U+2026 (?) ? but I’m  
> lazy.

_snore_ Sorry 🙂

-Martin

[Next page](https://www.ruby-forum.com/t/ruby-whitespace-semantics/54234.md?page=2)
