Whitespace syntax error?

Discussion in 'Ruby' started by Gregory Benjamin, Mar 6, 2004.

  1. 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.
     
    Gregory Benjamin, Mar 6, 2004
    #1
    1. Advertising

  2. Gregory Benjamin

    Mark Hubbart Guest

    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



    On Mar 6, 2004, at 1:29 PM, Gregory Benjamin wrote:

    > 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.
    >
    >
     
    Mark Hubbart, Mar 7, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oli Filth
    Replies:
    9
    Views:
    3,366
    Uncle Pirate
    Jan 17, 2005
  2. Replies:
    10
    Views:
    802
    Eric Brunel
    Dec 16, 2008
  3. MRAB
    Replies:
    3
    Views:
    408
  4. Greg Benjamin

    Whitespace Syntax Error?

    Greg Benjamin, Mar 6, 2004, in forum: Ruby
    Replies:
    4
    Views:
    94
    Gregory Benjamin
    Mar 6, 2004
  5. Mark Richards
    Replies:
    3
    Views:
    348
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page