Skip to content

File.head

Daniel Berger edited this page May 22, 2021 · 2 revisions

The File.head method is similar to the head command line tool in that it returns the first X lines of a file. In block form, yields the first num_lines (default: 10) from the file. In non-block form, returns an Array lines instead.

# Return an array
File.head('somefile.txt') # => ['This is line1', 'This is line2', ...]

# Return an array, first 20 lines
File.head('somefile.txt', 20) # => ['This is line1', 'This is line2', ...]

# Use a block
File.head('somefile.txt'){ |line| puts line }
Clone this wiki locally