[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

File.new error

Kai Vermehr

1/4/2006 1:03:00 PM

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!

--
Posted via http://www.ruby-....


4 Answers

Tim Hunter

1/4/2006 1:11:00 PM

0

Kai Vermehr wrote:
> 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!
>

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.

Robert Klemme

1/4/2006 1:17:00 PM

0

Timothy Hunter wrote:
> Kai Vermehr wrote:
>> 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!
>>
>
> 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

John Maclean

2/2/2006 1:34:00 PM

0

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>
wrote:

> 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!
>


--
John Maclean
MSc (DIC)
07739 171 531



Yukihiro Matsumoto

2/5/2006 3:45:00 PM

0

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