Last Updated: February 25, 2016
·
8.902K
· jayncoke

Get the First, Last, or N Line of a Text File in Ruby

# Secret sauce is .to_a
lines = File.open(filename).to_a
# Get the first line
puts lines.first
# Get the last line
puts lines.last
# Get the n line of a file
 puts lines.at(5)