Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Where is the definition of File.read()?

2 views
Skip to first unread message

optman

unread,
Sep 6, 2007, 12:20:09 PM9/6/07
to
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.

Jano Svitok

unread,
Sep 6, 2007, 12:33:00 PM9/6/07
to

Look in IO, or in io.c resp.

Gregory Brown

unread,
Sep 6, 2007, 12:34:05 PM9/6/07
to
On 9/6/07, optman <opt...@gmail.com> wrote:

It actually inherits IO.read, IIRC

Source:
http://www.ruby-doc.org/core/classes/IO.src/M002270.html

John Joyce

unread,
Sep 6, 2007, 1:24:40 PM9/6/07
to

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.


Tom Reilly

unread,
Sep 6, 2007, 1:59:41 PM9/6/07
to

Try:

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

add whatevery you want

end

file_in.close
>
>


optman

unread,
Sep 7, 2007, 12:41:02 AM9/7/07
to
On 9 7 , 1 24 , John Joyce <dangerwillrobinsondan...@gmail.com>
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?

rio4ruby

unread,
Sep 7, 2007, 1:15:27 AM9/7/07
to

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')

optman

unread,
Sep 7, 2007, 1:41:01 AM9/7/07
to

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.

rio4ruby

unread,
Sep 7, 2007, 2:00:34 AM9/7/07
to
> the reason.- Hide quoted text -
>
> - Show quoted text -

You might want to look at this:
http://devblog.famundo.com/articles/2007/03/10/improving-capistranos-put-command

optman

unread,
Sep 7, 2007, 3:52:10 AM9/7/07
to
> You might want to look at this:http://devblog.famundo.com/articles/2007/03/10/improving-capistranos-...

Good article!

I have solve the problem by read file in binary mode, as

body = File.open(tar_file, "rb") { |f| f.read }
put(body, tar_file)

more: http://groups.google.com/group/capistrano/browse_thread/thread/5e7e81525fd3993b

0 new messages