[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Can't create a new file

Nebs Petrovic

1/30/2009 2:44:00 AM

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.
--
Posted via http://www.ruby-....

3 Answers

Peña, Botp

1/30/2009 2:52:00 AM

0

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

Nebs Petrovic

1/30/2009 3:19:00 AM

0

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.
--
Posted via http://www.ruby-....

Robert Klemme

1/30/2009 12:37:00 PM

0

2009/1/30 Nebs Petrovic <nebs_man@hotmail.com>:
> 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

--
remember.guy do |as, often| as.you_can - without end