Forum: Ruby Can't create a new file

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nebs P. (Guest)
on 2009-01-30 04:46
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.
Peña, Botp (Guest)
on 2009-01-30 04:55
(Received via mailing list)
From: Nebs P. [mailto:removed_email_address@domain.invalid]
# myfile = File.new("somefile.txt", "r+")

myfile = File.new("somefile.txt", "w")
                                ^^^^^^^

# myfile.puts "Hello"
# myfile.close

try using the block form File.open since it opens and close for you, eg

create the file,

> File.open("somefile.txt", "w") do |f|
    f.puts "Hello"
  end
#=> nil

then read it back

> File.open("somefile.txt", "r") do |f|
    puts f.gets
  end
Hello
#=> nil
Nebs P. (Guest)
on 2009-01-30 05:21
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.
Robert K. (Guest)
on 2009-01-30 14:39
(Received via mailing list)
2009/1/30 Nebs P. <removed_email_address@domain.invalid>:
> 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
This topic is locked and can not be replied to.