Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

243
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda