Forum: Ruby File.new error

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.
4ffb2ca114218e4e52ded378eeff44cf?d=identicon&s=25 Carlos Delmar (cow6oy)
on 2006-01-04 14:02
Hi Forum,

I'm trying to create a file but I'm getting an error and can't find the
problem:

-------------------------
aFile = File.new("rubytestfile")

    puts "hello"

aFile.close
-------------------------

I get this error. I'm working on Mac OS X:

Errno::ENOENT: No such file or directory - rubytestfile

thanks for any help!
37a3c73ffbf864e4b28f7f2384ee12ce?d=identicon&s=25 Timothy Hunter (tim-hunter)
on 2006-01-04 14:16
(Received via mailing list)
Kai Vermehr wrote:
> aFile.close
> -------------------------
>
> I get this error. I'm working on Mac OS X:
>
> Errno::ENOENT: No such file or directory - rubytestfile
>
> thanks for any help!
>

Try

aFile = File.new("rubytestfile", "w")

If you don't specify the 2nd argument (the open mode) Ruby assumes you
want to open the file for reading only. If the file doesn't exist, you
can't read it.
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-01-04 14:19
(Received via mailing list)
Timothy Hunter wrote:
>>
> Try
>
> aFile = File.new("rubytestfile", "w")
>
> If you don't specify the 2nd argument (the open mode) Ruby assumes you
> want to open the file for reading only. If the file doesn't exist, you
> can't read it.

And please use the block form from the start, i.e.

File.open("rubytestfile", "w") do |aFile|
  aFile.puts "hello"
end

Btw, the original script wrote to stdout and not to the file.

Kind regards

    robert
508861510639b56d61eb30e6a28b01d9?d=identicon&s=25 John Maclean (Guest)
on 2006-02-02 14:35
(Received via mailing list)
Could this method be extended to acept input from stdin? Something
similar to:-

def testwrite
  File.open("testfile", "w") { |file| file.gets "test" }
end
testwrite

The intention is to read lines from stdnin and put them into the file...


On Wed, 4 Jan 2006 22:03:03 +0900 Kai Vermehr <k@eboy.com>
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2006-02-05 16:47
(Received via mailing list)
Hi,

In message "Re: File.new error"
    on Thu, 2 Feb 2006 22:33:35 +0900, John Maclean <info@jayeola.org>
writes:

|Could this method be extended to acept input from stdin? Something
|similar to:-
|
|def testwrite
|  File.open("testfile", "w") { |file| file.gets "test" }
|end
|testwrite
|
|The intention is to read lines from stdnin and put them into the file...

require 'fileutils'

def testwrite
  File.open("testfile", "w") {|file|
    FileUtils.copy_stream(STDIN, file)
  }
end
This topic is locked and can not be replied to.