From e3cb6518d1c4fb3fc74737f66785be419f162ea4 Mon Sep 17 00:00:00 2001 From: akr Date: Tue, 17 Mar 2009 01:37:57 +0000 Subject: [PATCH] rdoc update. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22985 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- io.c | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/io.c b/io.c index a6951e8191..817f6e6d1e 100644 --- a/io.c +++ b/io.c @@ -1967,9 +1967,23 @@ rb_io_write_nonblock(VALUE io, VALUE str) * call-seq: * ios.read([length [, buffer]]) => string, buffer, or nil * - * Reads at most length bytes from the I/O stream, or to the - * end of file if length is omitted or is nil. + * Reads length bytes from the I/O stream. + * * length must be a non-negative integer or nil. + * + * If length is a positive integer, + * It reads length bytes and + * returns a string which is length bytes long. + * If EOF is met after 1 or more bytes read but before length bytes read, + * a shorter string is returned. + * If EOF is met at beginning, nil is returned. + * + * If length is omitted or is nil, + * it reads until EOF. + * It returns a string even if EOF is met at beginning. + * + * If length is zero, it returns "". + * * If the optional buffer argument is present, it must reference * a String, which will receive the data. * @@ -1979,10 +1993,34 @@ rb_io_write_nonblock(VALUE io, VALUE str) * ios.read(nil) returns "". * ios.read(positive-integer) returns nil. * - * ios.read(0) returns "". - * * f = File.new("testfile") * f.read(16) #=> "This is line one" + * + * # reads whole file + * open("file") {|f| + * data = f.read # This returns a string even if the file is empty. + * ... + * } + * + * # iterate over fixed length records. + * open("fixed-record-file") {|f| + * while record = f.read(256) + * ... + * end + * } + * + * # iterate over variable length records. + * # record is prefixed by 32-bit length. + * open("variable-record-file") {|f| + * while len = f.read(4) + * len = len.unpack("N")[0] # 32-bit length + * record = f.read(len) # This returns a string even if len is 0. + * end + * } + * + * Note that this method behaves like fread function in C. + * If you need the behavior like read(2) system call, + * consider readpartial, read_nonblock and sysread. */ static VALUE