Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

IO#readlines

ios.readlines(sep=$/ [, getline_args]) → array
ios.readlines(limit [, getline_args]) → array
ios.readlines(sep, limit [, getline_args]) → array

Reads all of the lines in ios, and returns them in an array. Lines are separated by the optional sep. If sep is nil, the rest of the stream is returned as a single record. If the first argument is an integer, or an optional second argument is given, the returning string would not be longer than the given value in bytes. The stream must be opened for reading or an IOError will be raised.

Example
f = File.new("testfile")
f.readlines[0]   #=> "This is line one\n"

f = File.new("testfile", chomp: true)
f.readlines[0]   #=> "This is line one"

See IO.readlines for details about getline_args.

See also .readlines #readline #read_nonblock #readchar also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0