File.new ("write.txt", "w+") results in error

Discussion in 'Ruby' started by VMDD TECH, Aug 2, 2010.

  1. VMDD TECH

    VMDD TECH Guest

    Hello everyone,

    I am new to Ruby.

    If there are spaces between new and (, then ruby 1.9.1p376 considers
    it a syntactical error. For example:

    File.new ("write.txt", "w+")

    =>
    syntax error, unexpected ',', expecting ')'
    File.new ("write.txt", "w+")
    ^
    ....: syntax error, unexpected ')', expecting $end

    Is this a ruby bug or is it a feature/quirk of ruby?

    By the way, if I write as
    File.new "write.txt", "w+"
    File.new("write2.txt", "w+")

    then there are no problem.

    Thanks,

    Binh
     
    VMDD TECH, Aug 2, 2010
    #1
    1. Advertising

  2. On 08/02/2010 07:45 AM, VMDD TECH wrote:
    > Hello everyone,
    >
    > I am new to Ruby.
    >
    > If there are spaces between new and (, then ruby 1.9.1p376 considers
    > it a syntactical error. For example:
    >
    > File.new ("write.txt", "w+")
    >
    > =>
    > syntax error, unexpected ',', expecting ')'
    > File.new ("write.txt", "w+")
    > ^
    > ...: syntax error, unexpected ')', expecting $end
    >
    > Is this a ruby bug or is it a feature/quirk of ruby?
    >
    > By the way, if I write as
    > File.new "write.txt", "w+"
    > File.new("write2.txt", "w+")
    >
    > then there are no problem.
    >
    > Thanks,
    >
    > Binh
    >
    >

    It's not a bug. A comma-separated list inside of parentheses is just
    meaningless in Ruby, which is why you get a syntax error.

    Parentheses being "optional" for method calls is a feature, however.

    -Justin
     
    Justin Collins, Aug 3, 2010
    #2
    1. Advertising

  3. Eugen Ciur wrote:
    > File.new ("write.txt", "w+")
    >
    > =>
    > syntax error, unexpected ',', expecting ')'
    > File.new ("write.txt", "w+")
    > ^
    > ...: syntax error, unexpected ')', expecting $end
    >
    > Is this a ruby bug or is it a feature/quirk of ruby?


    The latter, because in ruby the parentheses surrounding the argument
    list are optional.

    Consider the difference here:

    puts (2-3).abs ## means: puts((2-3).abs)
    puts(2-3).abs ## means: (puts(2-3)).abs

    [The latter fails, because puts returns nil, and nil.abs is not defined]

    So if you include a space, the parentheses are considered part of the
    first argument. If you don't include a space, the parentheses are
    wrapping the argument list.

    In ruby 1.8,
    puts (2,3) ## accepted with warning
    puts (2,3).abs ## syntax error

    In ruby 1.9, they are both treated as a syntax error.
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Aug 4, 2010
    #3
    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. Monique Y. Mudama
    Replies:
    1
    Views:
    456
    Monique Y. Mudama
    Jun 28, 2005
  2. lovecreatesbeauty
    Replies:
    8
    Views:
    1,661
    Old Wolf
    Sep 12, 2005
  3. Ken Fine
    Replies:
    3
    Views:
    539
    Steven Cheng [MSFT]
    Jul 23, 2008
  4. Targa
    Replies:
    1
    Views:
    163
    Randy Webb
    Jun 6, 2004
  5. Replies:
    2
    Views:
    467
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page