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)
Written by Jay Aniceto
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#