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
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("<HTML></HTML>")
=> 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
<HTML></HTML>
···
From: VMDD TECH <binhph@gmail.com>
Organization: http://groups.google.com
Reply-To: <ruby-talk@ruby-lang.org>
Newsgroups: comp.lang.ruby
Date: Mon, 2 Aug 2010 23:45:09 +0900
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Subject: 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
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
···
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
Excerpt from The Ruby Programming 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
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/\.