thesecretmaster
thesecretmaster

Reputation: 1984

How do some files not execute when required in irb?

If I have file.rb:

puts "Hello, World"

then in irb type:

require "./file.rb"

the output will be Hello, World.

Why then, if I have a sinatra file, e.g.

require "sinatra"
get "/" do
    return "Hi"
end

and require that, there is no output?

Clarification

What executing the sinatra file via ruby sinatra_app.rb it will start a rack server, and not stop until CTRL+C is pressed. Why does it not do that when required in irb, but it does do that when it is explicitly run with ruby sinatra_app.rb?

Upvotes: 1

Views: 74

Answers (2)

olmstad
olmstad

Reputation: 706

Workaround is require sinatra before requiring file.

Root file:

require "sinatra"
require "/tmp/ddd.rb"

Required file:

get "/" do
    return "Hi"
end

I guess it's somehow related to Sinatra startup process. They put get method in default namespace, without namespacing it to separate module.

Upvotes: 0

Jörg W Mittag
Jörg W Mittag

Reputation: 369624

Because the script doesn't output anything. There is nothing in the script you showed that would generate any sort of output, there are no calls to print, puts, or p, no writes to any file, nothing.

The first script prints something when required, because it prints something, the second prints nothing when required because, well, it prints nothing. Remove the call to puts from the first script and it won't print anything either. Add a call to puts to the second script and it will print something.

Upvotes: 1

Related Questions