Whitespace syntax error?

  • Thread starter Gregory Benjamin
  • Start date
G

Gregory 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.
 
M

Mark Hubbart

Hi,
It's not a bug, it's expected behavior. The problem is that when you
are passing a block, it must begin on the same line as the function you
are passing it to. It's almost like an argument. So, you you have three
choices:

- put the opening brace on the function call's line, as in the first
example;
- use do...end instead of braces (since braces were really intended for
single-line code anyway (please don't flame me!!!!!))
- petition Matz?

Ruby is not whitespace-neutral. It uses newlines as statement
separators.

--Mark
 

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

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top