Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
Why this simple ruby code does not work in the commandline, but does when pasting it in irb

I have the following code:

sample_code.rb

class Foo
  def bar
    Timeout.timeout(0.5){
        puts "Interupt this if it takes longer then 0.5 seconds"
    }
  end
end

foo = Foo.new()
foo.bar

The example above works when you paste it in irb, but when you place it in a script and run it like this:

ruby ./sample_code.rb

It will give the following error.

Traceback (most recent call last):
        1: from ./irb_works_ruby_dont.rb:11:in `<main>'
./irb_works_ruby_dont.rb:4:in `bar': uninitialized constant Foo::Timeout (NameError)

Is this a Timeout issue? Does irb load some modules that the normal ruby command doesn't? How to make the code work when run as a script?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The most likely explaination is that IRB requires Timeout when it starts up the REPL - but your script file is being executed before that happens. You can fix it by simply requiring it:

require 'timeout'

class Foo
  def bar
    Timeout.timeout(0.5){
        puts "Interupt this if it takes longer then 0.5 seconds"
    }
  end
end

foo = Foo.new
foo.bar
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!