Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 852089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:32:13+00:00 2026-05-15T07:32:13+00:00

I’ve been working on a log viewer for a Rails app and have found

  • 0

I’ve been working on a log viewer for a Rails app and have found that I need to read around 200 lines of a log file from bottom to top instead of the default top to bottom.

Log files can get quite large, so I’ve already tried and ruled out the IO.readlines(“log_file.log”)[-200..-1] method.

Are there any other ways to go about reading a file backwards in Ruby without the need for a plugin or gem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:32:14+00:00Added an answer on May 15, 2026 at 7:32 am

    The only correct way to do this that also works on enormous files is to read n bytes at a time from the end until you have the number of lines that you want. This is essentially how Unix tail works.

    An example implementation of IO#tail(n), which returns the last n lines as an Array:

    class IO
      TAIL_BUF_LENGTH = 1 << 16
    
      def tail(n)
        return [] if n < 1
    
        seek -TAIL_BUF_LENGTH, SEEK_END
    
        buf = ""
        while buf.count("\n") <= n
          buf = read(TAIL_BUF_LENGTH) + buf
          seek 2 * -TAIL_BUF_LENGTH, SEEK_CUR
        end
    
        buf.split("\n")[-n..-1]
      end
    end
    

    The implementation is a little naive, but a quick benchmark shows what a ridiculous difference this simple implementation can already make (tested with a ~25MB file generated with yes > yes.txt):

                                user     system      total        real
    f.readlines[-200..-1]   7.150000   1.150000   8.300000 (  8.297671)
    f.tail(200)             0.000000   0.000000   0.000000 (  0.000367)
    

    The benchmark code:

    require "benchmark"
    
    FILE = "yes.txt"
    
    Benchmark.bmbm do |b|
      b.report "f.readlines[-200..-1]" do
        File.open(FILE) do |f|
          f.readlines[-200..-1]
        end
      end
    
      b.report "f.tail(200)" do
        File.open(FILE) do |f|
          f.tail(200)
        end
      end
    end
    

    Of course, other implementations already exist. I haven’t tried any, so I cannot tell you which is best.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.