Whitespace Syntax Error?

I’m using: ruby 1.8.1 (2004-01-27) [i386-mswin32]

When I run the following program it works fine:

···

####################################################
require ‘tk’

root = TkRoot.new { title “Example 1” }

TkLabel.new(root){
text "Hello, World!"
pack { padx 15; pady 15; side ‘left’ }
}

Tk.mainloop
####################################################
But when I run this version:
####################################################
require ‘tk’

root = TkRoot.new { title “Example 1” }

TkLabel.new(root)
{
text "Hello, World!"
pack { padx 15; pady 15; side ‘left’ }
}

Tk.mainloop
####################################################
I get the following:

hello.rbw:7: syntax error
text “Hello, World!”
^
hello.rbw:7: warning: unused literal ignored
hello.rbw:9: syntax error
####################################################
The only difference between the two versions is the
insertion of a CR/LF between “)” and “{” on line 7.
I thought Ruby was whitespace neutral. What is wrong
here?

I care about this because I like to line up my braces
as shown in the second example.

> The only difference between the two versions is the > insertion of a CR/LF between ")" and "{" on line 7. > I thought Ruby was whitespace neutral. What is wrong > here?

20 million will answer this ahead of me probably, but remember…
newline means end-of-statement in ruby. It needs to be on the same
line, or escape the end of the previous line (use a backslash).

···

On Sun, 7 Mar 2004 05:54:39 +0900 “Greg Benjamin” gregbenx@yahoox.com wrote:


Ryan Pavlik rpav@mephle.com

“Yes, do I not now know who… don’t not know… shutup!” - 8BT

“Greg Benjamin” gregbenx@yahoox.com writes:

The only difference between the two versions is the
insertion of a CR/LF between “)” and “{” on line 7.
I thought Ruby was whitespace neutral. What is wrong
here?

I care about this because I like to line up my braces
as shown in the second example.

It’s whitespace neutral where it’s unambiguous. The problem here is
that \n serves as a statement separator, so:

TkLabel.new(root)

Here we’re calling a function without a block…

{

…and here we’re starting a Hash.

text “Hello, World!”
pack { padx 15; pady 15; side ‘left’ }
}

HTH

Greg Benjamin wrote:

TkLabel.new(root)
{
text “Hello, World!”
pack { padx 15; pady 15; side ‘left’ }
}

Tk.mainloop
####################################################
I get the following:

hello.rbw:7: syntax error
text “Hello, World!”
^
hello.rbw:7: warning: unused literal ignored
hello.rbw:9: syntax error
####################################################
The only difference between the two versions is the
insertion of a CR/LF between “)” and “{” on line 7.
I thought Ruby was whitespace neutral. What is wrong
here?

I care about this because I like to line up my braces
as shown in the second example.

Not possible, AFAIK.

Since the expression on the line with the call to TkLabel.new is
considered complete by the parser, the block below is not attached to
the call. I’d guess Ruby then thinks you are entering a Hash literal,
which would explain the syntax error.

An attached block must begin on the same line as the method call. You
could perhaps add a \ to the end of the line with the method call, but
that is quite ugly.

As for lining up braces, keep in mind that those braces are block
delimiters and not your run of the mill compound statement delimiters
like in C/C++, Java etc.

···


([ Kent Dahl ]/)_ ~ [ Kent Dahl - Kent Dahl ]/~
))_student_/(( _d L b_/ Master of Science in Technology )
( __õ|õ// ) ) Industrial economics and technology management (
_
/ö____/ (_engineering.discipline=Computer::Technology)

Thanks everyone, for the quick reply!
Also, sorry about the duplicate posting. I got a message
seemed to indicate that the OP was never sent due to
an error.

As for my solution, I’ll put a ‘’ on the end of
the line at the start of the block when the block
itself “looks nicer” (to me) spread over several
lines rather than all in one line:

···

from my news posting software (Outlook Express) that

#####################################################
require ‘tk’

root = TkRoot.new { title “Example 1” }

TkLabel.new(root)
{
text "Hello, World!"
pack { padx 15; pady 15; side ‘left’ }
}

Tk.mainloop
#####################################################

This satisfies my incurable urge to line up braces
in neat columns. :slight_smile: