File.new : having problems in windows

Hi all,

I’m a relatively new user to ruby. I was trying to use the File.new
and was using a directory path as well. It seems like ruby wants to
replace the \ (backslash) for the directory paths with a double
backslash \.

How can I get around this or fix this problem?

···
         directory = "\\blah\\test.txt"
         printf ("This is string stores %s \n\n",directory)
         d = File.new(directory,"w+")

Output:

This is string stores \blah\test.txt

parser.rb:20:in initialize': No such file or directory - "\\blah\\test.txt" NOENT) from parser.rb:20:innew’
from parser.rb:20


It looks like the directory name is stored correctly but the file.new
function tries to add an extra \ into the directory names.

Thanks,

  • Khurram

Hi all,

I’m a relatively new user to ruby. I was trying to use the File.new
and was using a directory path as well. It seems like ruby wants to
replace the \ (backslash) for the directory paths with a double
backslash \.

How can I get around this or fix this problem?

You should either use single quotes to store filenames (so the
backslashes aren’t interpreted by ruby), or use forward slashes as your
path delimiter (if I remmeber correctly, that also works correctly in
Windows).

So, instead of

directory = “\blah\test.txt”

Use single quotes:

directory = ‘\blah\test.txt’

or forward slashes

directory ‘/blah/test.txt’


         directory = "\\blah\\test.txt"
         printf ("This is string stores %s \n\n",directory)
         d = File.new(directory,"w+")

Output:

This is string stores \blah\test.txt

parser.rb:20:in initialize': No such file or directory - "\\blah\\test.txt" NOENT) from parser.rb:20:in new’
from parser.rb:20


It looks like the directory name is stored correctly but the file.new
function tries to add an extra \ into the directory names.

It’s not adding an extra backslash; the error handing is calling inspect
on the string, which escapes it. For example:

irb(main):003:0> puts “\asdf”.inspect, ‘\asdf’.inspect
“\asdf”
“\asdf”

The problem you’re having is exactly what the error message says: there
is no file or directory called ‘\blah\test.txt’. I have no experience
running ruby under Windows, but my best guess would be to try either
‘\c\blah\test.txt’ or ‘/c/blah/test.txt’.

···

Thanks,

  • Khurram


Paul Duncan pabs@pablotron.org pabs in #gah (OPN IRC)
http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562