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 8931777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:11:13+00:00 2026-06-15T09:11:13+00:00

Is there a way in Ruby to have it print the __LINE__ number of

  • 0

Is there a way in Ruby to have it print the __LINE__ number of code (at my script level, not required gems) it’s working on if taking longer than 9 seconds (adjustable)?

For debugging I am getting it to print verbose output of what it’s trying to do, where it is in the code etc., rather than silently sitting for long periods of time.

A flaky situation makes it unpredicable how far it gets before something times out, so successive advancing doesn’t apply here.

EDIT

Something like a trap would work, such that:

  • The original line number and hopefully code get remembered (both benchmark and timeout gems lose track of __LINE__ for instance…. Maybe there is a way to push it off to another .rb file to manipulate the stack to include my file & line of interest?)
  • When the overtime warning prints, execution still continues as if nothing had changed.
  • 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-06-15T09:11:14+00:00Added an answer on June 15, 2026 at 9:11 am
    require 'timeout'
    
    def do_something
      Timeout::timeout(9) do
        sleep 10
      end
    rescue Timeout::Error => e
      puts "Something near line #{__LINE__} is taking too long!"
      # or, count backwards in method
      puts "Line #{__LINE__ - 5} is taking too long!"
    end
    
    do_something
    

    This will stop execution if the timeout block runs out of time and raise a Timeout error.
    If you want to continue execution, you might do better with benchmark:

    require 'benchmark'
    
    time = Benchmark.realtime do
      sleep 10
    end
    puts "Line #{__LINE__ - 2} is slow" if time > 9
    

    One benchmark block can have multiple timers:

    Benchmark.bm do |b|
      b.report('sleeping:') { sleep 3 }
      b.report('chomping:') { " I eat whitespace ".chomp }
    end
    

    See more about benchmark here:
    http://ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html

    If you want to keep track of the line number being executed, why don’t you try passing it in to a custom method like so:

    def timethis(line, &block)
      if Benchmark.realtime(&block) > 2
        puts "Line #{line} is slow"
      end
    end
    
    timethis(__LINE__) { sleep 1 }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way in a Ruby script to sftp to a server and
Is there a way to print out the memory location of a Ruby object,
Is there a simple way in Ruby to get a true/false value from something
is there any way I can reproduce this ruby function: def Password.hash(password,salt) Digest::SHA512.hexdigest(#{password}:#{salt}) end
Is there a way using the Net::SFTP Library in Ruby ( API Link )
Is there a way to make emacs pull autocompletions of ruby methods the way
In Ruby, is there a way to get the implicit object of a case
With Ruby on Rails, is there a way for me to dump my production
I am new to Ruby, is there a way to yield values from Ruby
Ruby 1.8.7: abc[0] => 65 Ruby 1.9* abc[0] => a Is there a way

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.