Forum: Ruby File.new raise an exception ?

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.
492269684bb2e22108c34be6afb27d8f?d=identicon&s=25 Jean-François Boisvieux (Guest)
on 2006-03-26 17:51
(Received via mailing list)
Hello,

I'm pretty new to ruby and I've written some code to parse apache
logs, it works but now I want to store the result in a file. The
problem is as follow (using irb)

irb(main):001:0> foo = File.open("test")
Errno::ENOENT: No such file or directory - test
         from (irb):1:in `initialize'
         from (irb):1:in `open'
         from (irb):1

I just do not understand the problem ( I can create a file with
python in the same directory ), what's wrong for me ?

thanks for a quick answer

Jean-François
5ba9d3f0aafd95a94ef6f0e90a659411?d=identicon&s=25 Patrick Hof (Guest)
on 2006-03-26 18:39
(Received via mailing list)
Jean-François Boisvieux <jfb@mxs.fr> schrieb:

> irb(main):001:0> foo = File.open("test")
> Errno::ENOENT: No such file or directory - test
>         from (irb):1:in `initialize'
>         from (irb):1:in `open'
>         from (irb):1
>
> I just do not understand the problem ( I can create a file with
> python in the same directory ), what's wrong for me ?

Ruby is opening the file for reading per default. As there seems to be
no file with the name test, you get the error. To open it for writing,
you have to say

File.open("test", "w")

Patrick
492269684bb2e22108c34be6afb27d8f?d=identicon&s=25 Jean-François Boisvieux (Guest)
on 2006-03-26 18:58
(Received via mailing list)
Thanks Patrick for this precise and clear answer

Jean-Francois

Le 26 mars 06 à 18:38, Patrick Hof a écrit :
This topic is locked and can not be replied to.