U:RDoc::AnyMethod[iI" read:ETI" IO#read;TF: publico:RDoc::Markup::Document: @parts[o:RDoc::Markup::Paragraph; [I".Reads _length_ bytes from the I/O stream.;To:RDoc::Markup::BlankLineo; ; [I"6_length_ must be a non-negative integer or +nil+.;T@o; ; [ I"=If _length_ is a positive integer, +read+ tries to read ;TI":_length_ bytes without any conversion (binary mode). ;TI"LIt returns +nil+ if an EOF is encountered before anything can be read. ;TI"LFewer than _length_ bytes are returned if an EOF is encountered during ;TI"the read. ;TI"HIn the case of an integer _length_, the resulting string is always ;TI"in ASCII-8BIT encoding.;T@o; ; [I""").;T@o; ; [ I"3If the optional _outbuf_ argument is present, ;TI">it must reference a String, which will receive the data. ;TI"LThe _outbuf_ will contain only the received data after the method call ;TI".even if it is not empty at the beginning.;T@o; ; [ I"AWhen this method is called at end of file, it returns +nil+ ;TI"0or "", depending on _length_: ;TI"E+read+, read(nil), and read(0) return ;TI""", ;TI">read(positive_integer) returns +nil+.;T@o:RDoc::Markup::Verbatim; [I"f = File.new("testfile") ;TI")f.read(16) #=> "This is line one" ;TI" ;TI"# read whole file ;TI"open("file") do |f| ;TI"J data = f.read # This returns a string even if the file is empty. ;TI" # ... ;TI" end ;TI" ;TI")# iterate over fixed length records ;TI"&open("fixed-record-file") do |f| ;TI"" while record = f.read(256) ;TI" # ... ;TI" end ;TI" end ;TI" ;TI"-# iterate over variable length records, ;TI"4# each record is prefixed by its 32-bit length ;TI")open("variable-record-file") do |f| ;TI" while len = f.read(4) ;TI"4 len = len.unpack("N")[0] # 32-bit length ;TI"N record = f.read(len) # This returns a string even if len is 0. ;TI" end ;TI" end ;T: @format0o; ; [ I"CNote that this method behaves like the fread() function in C. ;TI"GThis means it retries to invoke read(2) system calls to read data ;TI"/with the specified length (or until EOF). ;TI"LThis behavior is preserved even if ios is in non-blocking mode. ;TI"F(This method is non-blocking flag insensitive as other methods.) ;TI"AIf you need the behavior like a single read(2) system call, ;TI"9consider #readpartial, #read_nonblock, and #sysread.;T: @fileI" io.c;T:0@omit_headings_from_table_of_contents_below0I"@ios.read([length [, outbuf]]) -> string, outbuf, or nil ;T0[I"(p1 = v1, p2 = v2);T@SFI"IO;TcRDoc::NormalClass00