[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby Whitespace Semantics

Almann Goo

2/27/2006 7:44:00 AM

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
19 Answers

Hal E. Fulton

2/27/2006 7:53:00 AM

0

Almann Goo wrote:
> 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.

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.



Kev Jackson

2/27/2006 7:53:00 AM

0

Almann Goo wrote:

>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
>
>
>
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


Almann Goo

2/27/2006 8:13:00 AM

0

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

Alexandru E. Ungur

2/27/2006 9:24:00 AM

0

>>> sender: "Almann Goo" date: "Mon, Feb 27, 2006 at 04:43:35PM +0900" <<<EOQ
> 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.
This behaviour is documented, at least here:
http://www.rubycentral.com/book/lan...

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


Almann Goo

2/27/2006 3:20:00 PM

0

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

Avdi Grimm

2/27/2006 5:15:00 PM

0

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

On 2/27/06, Almann Goo <almann.goo@gmail.com> wrote:
>
> 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
>
>
>

Mark Wilden

2/27/2006 6:57:00 PM

0

"Hal Fulton" <hal9000@hypermetrics.com> wrote in message
news:4402AFA0.5080703@hypermetrics.com...
>
> 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)

But (4 is clearly an incomplete statement.


Jacob Fugal

2/27/2006 7:03:00 PM

0

On 2/27/06, Mark Wilden <mark@mwilden.com> wrote:
> "Hal Fulton" <hal9000@hypermetrics.com> wrote:
> > 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)
>
> 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 Fugal


Anthony DeRobertis

2/27/2006 7:21:00 PM

0

Tim Hunter

2/27/2006 7:47:00 PM

0

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."