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

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

On 08/02/2010 07:45 AM, VMDD TECH wrote:

syntax error, unexpected ‘,’, expecting ‘)’
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

When using ( spaces are not required in 1.8.7 the error is
(irb):1: warning: don’t put space before argument parentheses

In 1.9.2
SyntaxError: (irb):1: syntax error, unexpected ‘,’, expecting ‘)’
f = File.open (“l.txt”, “w+”)

1.9.2 appears the error is a little obscure…

MANISH.local:jes [08-02] 0 517:17 (295.6 Mb) •
! irb
irb(main):001:0> f = File.open (“l.txt”, “w+”)
(irb):1: warning: don’t put space before argument parentheses
=> #<File:l.txt>
irb(main):002:0> f = File.open(“l.txt”, “w+”)
=> #<File:l.txt>
irb(main):003:0> f.write("")
=> 13
irb(main):004:0> f.close
=> nil
irb(main):005:0> exit

MANISH.local:jes [08-02] 0 518:18 (295.6 Mb) •
! cat l.txt

Eugen C. 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.

Excerpt from The Ruby P.ming Language book:
-----------------------------------------------------------------------------
When you use parentheses in a method invocation, the opening parenthesis must immediately follow the method name, with no space.
 This is because parentheses can be used around an argument list in a method invocation, and they can be used for grouping expressions.
Consider the following two examples:
square(2+2)*2    # square(4)*2 = 16*2 = 32
square (2+2)*2   # square(4*2) = square(8) = 64
In the first expression, the parentheses represent method invocation. In the second, they represent expression grouping.
To reduce the potential for confusion, you should always use parentheses around a method invocation if any of the arguments use parentheses.
The second expression would be written more clearly as:
square((2+2)*2)
-----------------------------------------------------------------------------
In Ruby 1.8.x interpreter issued an warning, in Ruby 1.9.y they decided to issue an error when interpreter encounters something like foo (args).

-----
Eugen

On 08/02/2010 05:45 PM, 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs