Can't create a new file

N

Nebs Petrovic

I have no idea what's going on. I've read about a dozen forums about
creating a file but I still can't seem to do it.

I'm running in Windows Vista.
In a folder called "RUBY" I have a ruby file called "filetest.rb".
There are no restrictions on the "RUBY" directory.

In the ruby file there are only 3 lines:
myfile = File.new("somefile.txt", "r+")
myfile.puts "Hello"
myfile.close

When I run "ruby filetest.rb" I get the following error:
filetest.rb:5:in `initialize': No such file or directory - somefile.txt
(Errno::
ENOENT)
from filetest.rb:5:in `new'
from filetest.rb:5


I know there is "no such file", that's why I'm creating it. *scratches
head*. If I create the file manually then it works, but I can't get it
to create the file from scratch. I passed no folder structure in the
arguments so I assume that it should try and create the file in the same
directory of the *.rb file. What am I missing?

Thanks.
 
P

Peña, Botp

RnJvbTogTmVicyBQZXRyb3ZpYyBbbWFpbHRvOm5lYnNfbWFuQGhvdG1haWwuY29tXSANCiMgbXlm
aWxlID0gRmlsZS5uZXcoInNvbWVmaWxlLnR4dCIsICJyKyIpDQoNCm15ZmlsZSA9IEZpbGUubmV3
KCJzb21lZmlsZS50eHQiLCAidyIpDQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIF5e
Xl5eXl4NCg0KIyBteWZpbGUucHV0cyAiSGVsbG8iDQojIG15ZmlsZS5jbG9zZQ0KDQp0cnkgdXNp
bmcgdGhlIGJsb2NrIGZvcm0gRmlsZS5vcGVuIHNpbmNlIGl0IG9wZW5zIGFuZCBjbG9zZSBmb3Ig
eW91LCBlZw0KDQpjcmVhdGUgdGhlIGZpbGUsDQoNCj4gRmlsZS5vcGVuKCJzb21lZmlsZS50eHQi
LCAidyIpIGRvIHxmfA0KICAgIGYucHV0cyAiSGVsbG8iDQogIGVuZA0KIz0+IG5pbA0KDQp0aGVu
IHJlYWQgaXQgYmFjaw0KDQo+IEZpbGUub3Blbigic29tZWZpbGUudHh0IiwgInIiKSBkbyB8ZnwN
CiAgICBwdXRzIGYuZ2V0cw0KICBlbmQNCkhlbGxvDQojPT4gbmlsDQoNCg0K
 
N

Nebs Petrovic

Thanks, it works now. It had to do with opening for writing only. I
guess it makes sense that you can't open a file for reading if it
doesn't exist, I just thought that if you open for reading AND writing
it would be smart enough to create it anyways and just return nil on a
"gets" or similar request. Anyways, thanks again.
 
R

Robert Klemme

2009/1/30 Nebs Petrovic said:
Thanks, it works now. It had to do with opening for writing only. I
guess it makes sense that you can't open a file for reading if it
doesn't exist, I just thought that if you open for reading AND writing
it would be smart enough to create it anyways and just return nil on a
"gets" or similar request. Anyways, thanks again.

Well, you can do that but you need a different open mode: open for
writing /plus/ reading:

13:37:55 ~$ ls -l foo
ls: cannot access foo: No such file or directory
13:37:58 ~$ irb
Ruby version 1.8.7
irb(main):001:0> File.open("foo","w+") {|io|
io.puts("test");io.seek(0,IO::SEEK_SET); p io.read}
"test\n"
=> nil
irb(main):002:0> exit
13:38:10 ~$ ls -l foo
-rw-r--r-- 1 RKlemme Domain Users 5 Jan 30 13:38 foo
13:38:11 ~$

Kind regards

robert
 

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

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top