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

V

VMDD TECH

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
 
J

Justin Collins

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
 
B

Brian Candler

Eugen said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top