Skip to content

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

class method

IO.readlines

IO.readlines(name, sep=$/ [, getline_args, open_args]) → array
IO.readlines(name, limit [, getline_args, open_args]) → array
IO.readlines(name, sep, limit [, getline_args, open_args]) → array

Reads the entire file specified by name as individual lines, and returns those lines in an array. Lines are separated by sep.

Example
a = IO.readlines("testfile")
a[0]   #=> "This is line one\n"

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

If the last argument is a hash, it’s the keyword argument to open.

Options for getline

The options hash accepts the following keys:

:chomp

When the optional chomp keyword argument has a true value, \n, \r, and \r\n will be removed from the end of each line.

See also IO.read for details about open_args.

See also #readlines .read .select .popen also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1