Whitespace Syntax Error?

G

Greg Benjamin

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

George Ogata

Greg Benjamin said:
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
 
R

Ryan Pavlik

On Sun, 7 Mar 2004 05:54:39 +0900

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).
 
K

Kent Dahl

Greg said:
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.
 
G

Gregory Benjamin

Thanks everyone, for the quick reply!
Also, sorry about the duplicate posting. I got a message
from my news posting software (Outlook Express) that
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:

#####################################################
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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top