Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda