Where is the definition of File.read()?

O

optman

Where is the public class method File.read() defined? Which take a
file name as parameter, but what is in return? I have search all the
document and source code, just can't find the definition.

Could anyone point me to the source file? I really want to know,
thanks in advance.
 
J

Jano Svitok

Where is the public class method File.read() defined? Which take a
file name as parameter, but what is in return? I have search all the
document and source code, just can't find the definition.

Could anyone point me to the source file? I really want to know,
thanks in advance.

Look in IO, or in io.c resp.
 
J

John Joyce

Ruby methods normally return the last thing in the method.
File.read() will return the contents of the file.

As others said, it inherits from class IO
Many people just use IO.read() instead because of this.
you can go into the site ruby directory inside of the ruby directory
(where you installed ruby) and open the File class files and read them.
 
T

Tom Reilly

optman said:
Where is the public class method File.read() defined? Which take a
file name as parameter, but what is in return? I have search all the
document and source code, just can't find the definition.

Could anyone point me to the source file? I really want to know,
thanks in advance.

Try:

file_in = File.new("input_file","r")
file_in.each do |line|

add whatevery you want

end

file_in.close
 
O

optman

Ruby methods normally return the last thing in the method.
File.read() will return the contents of the file.

As others said, it inherits from class IO
Many people just use IO.read() instead because of this.
you can go into the site ruby directory inside of the ruby directory
(where you installed ruby) and open the File class files and read them.

I have seen a line of code like

put( File.read(sourceFile),targetFile)

if File.read() return the entire file content, what if sourceFile is a
large file?
 
R

rio4ruby

On 9 7 , 1 24 , John Joyce <[email protected]>
wrote:

I have seen a line of code like

put( File.read(sourceFile),targetFile)

if File.read() return the entire file content, what if sourceFile is a
large file?

Then you are in trouble if you use File.read(). From your example I
can don't know what the method #put does, but one might logically
surmise that its function is to put a string (the output of File.read)
into targetFile.

Reading a file into a string and then writing that string to a file is
not a good solution for large files. The module ::FileUtils contains a
method #cp which very efficiently (by ruby standards) copies one file
to another -- without requiring that the entire contents of the source
file be copied into memory first.

Rio (http://rio.rubyforge.org) will also copy a file without requiring
the source file being read into memory first:

rio('sourceFile') > rio('targetFile')
 
O

optman

Then you are in trouble if you use File.read(). From your example I
can don't know what the method #put does, but one might logically
surmise that its function is to put a string (the output of File.read)
into targetFile.

Reading a file into a string and then writing that string to a file is
not a good solution for large files. The module ::FileUtils contains a
method #cp which very efficiently (by ruby standards) copies one file
to another -- without requiring that the entire contents of the source
file be copied into memory first.

Rio (http://rio.rubyforge.org) will also copy a file without requiring
the source file being read into memory first:

rio('sourceFile') > rio('targetFile')

Method #put is from Capistrano, which upload file to remote server by
ssh. I found it don't work correctly with large file, maybe this is
the reason.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,006
Latest member
MelinaLema

Latest Threads

Top