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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:52:12+00:00 2026-05-20T04:52:12+00:00

The following code require ‘timeout’ begin timeout(20) do # Line 4 result = `hostname`

  • 0

The following code

require 'timeout'

begin
  timeout(20) do              # Line 4
    result = `hostname`
  end                         # Line 6
rescue Timeout::Error
  puts "Timeout"
  exit
end

puts "Result:" + result       # Line 12

throws the error

issue.rb:12:in <main>': undefined local variable or methodresult’ for
main:Object (NameError)

but if I comment out the timeout element (lines 4 and 6), it works fine. I have tried using IO.popen, IO.select etc but none of this helps. I’ve used this timeout technique in many other areas and it worked fine.

It doesn’t appear to be related to the timeout value as I have experimented with much larger and smaller values.

Am using Ruby 1.92 on Windows XP. Any help much appreciated.

p.s. My original problem was not running “hostname” but a more complex SQL Server batch job. As a bonus point, would a long running system task that exceeded the timeout be automatically killed? I have read lots of posts about the timeout library not honouring timeouts when busy running system tasks?

  • 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-20T04:52:13+00:00Added an answer on May 20, 2026 at 4:52 am

    The result variable is being defined inside the timeout block, so it’s not visible in the outer scope. You should initialize it before:

    result = nil 
    begin
      timeout(20) do              # Line 4
        result = `hostname`
      end                         # Line 6
    rescue Timeout::Error
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting an error with the following code when trying to send an email.
I am using following code and want to know whether we require to set
I've been reviewing a sample code to learn PHP and what I saw made
I am having trouble with the most simple piece of code. For some reason,
I'm not good with PHP, so please bear with me. I have the following
in my iOS app I do the following. viewDidAppear(){ // Load a spinner in
I've just started using Ruby on Rails and so far it's working nicely. I'm
I have an NodeJS Express app that is getting really big in just one
In a previous question I figured out how to start a password-authenticated ssh sessions
I am creating a web application with Ruby on Rails 3.1 (RC1). I am

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.