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

**URL:** <https://www.ruby-forum.com/t/file-new-write-txt-w-results-in-error/192581>\
**Category:** Ruby\
**Created:** [August 2, 2010, 4:45pm UTC](https://www.ruby-forum.com/t/file-new-write-txt-w-results-in-error/192581 "2010-08-02T16:45:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![VMDD\_TECH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/919ad9/32.png) [@VMDD\_TECH](https://www.ruby-forum.com/u/VMDD_TECH)\
**Post date:** [August 2, 2010, 4:45pm UTC](https://www.ruby-forum.com/t/file-new-write-txt-w-results-in-error/192581/1 "2010-08-02T16:45:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![VMDD\_TECH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/919ad9/32.png) [@VMDD\_TECH](https://www.ruby-forum.com/u/VMDD_TECH)\
**Post date:** [August 3, 2010, 5:16am UTC](https://www.ruby-forum.com/t/file-new-write-txt-w-results-in-error/192581/2 "2010-08-03T05:16:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![VMDD\_TECH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/919ad9/32.png) [@VMDD\_TECH](https://www.ruby-forum.com/u/VMDD_TECH)\
**Post date:** [August 2, 2010, 6:23pm UTC](https://www.ruby-forum.com/t/file-new-write-txt-w-results-in-error/192581/3 "2010-08-02T18:23:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![VMDD\_TECH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/919ad9/32.png) [@VMDD\_TECH](https://www.ruby-forum.com/u/VMDD_TECH)\
**Post date:** [August 4, 2010, 11:51am UTC](https://www.ruby-forum.com/t/file-new-write-txt-w-results-in-error/192581/4 "2010-08-04T11:51:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![VMDD\_TECH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/919ad9/32.png) [@VMDD\_TECH](https://www.ruby-forum.com/u/VMDD_TECH)\
**Post date:** [August 3, 2010, 8:20am UTC](https://www.ruby-forum.com/t/file-new-write-txt-w-results-in-error/192581/5 "2010-08-03T08:20:05Z")

</div>

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.  
&nbsp;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
> ```
