Why sinatra won't load if 'require sinatra' is in another file?

require, ruby

Solution

When you run a Sinatra program directly with something like `ruby my_app.rb`, Sinatra can start a web server automatically. Sinatra determines whether to run this built-in server by checking whether the `:run` setting is `true`.

The default for value `:run` is determined by checking if the `:app_file` setting is the same as the Ruby file being executed, i.e. `$0`.

Sinatra determines the default value for `:app_file` by using the first file that executes `require 'sinatra'`. In this case, when you have `require 'sinatra'` at the top of `test.rb` then `:app_file` is `$0`, so Sinatra starts the built-in server. When you use `test_require.rb`, `$0` is `test.rb`, but the file to require Sinatra, and therfore `:app_file`, is `test_require.rb`, and as they don’t match the built-in server isn’t started.

To fix this you can explicitly set `:app_file` in your `test.rb` (or `web.rb` or wherever):

set :app_file, __FILE__

You’ll need to do this after you’ve required Sinatra. You could also directly set `:run`, although this would be less flexible as it would run the built-in server every time, which you wouldn’t want if you’re using `config.ru` for example:

enable :run

Although this should solve your problem, personally I think it would be better to keep the `require 'sinatra'` together with the Sinatra code.

Problem

I have web.rb file that works fine. I wanted to move all require files in one requires.rb file and call this file from all my .rb files and use conditions based on a caller file name to require what is needed. I tested it on 'require pp', 'ruby-growl' and and it worked fine. But sinatra won't load if the `require sinatra` is in another file. Can I have `require sinatra` in a different file? Or better in general can I have require in different file? using - ruby 1.8.7 (2010-08-16 patchlevel 302) [i386-mingw32] - sinatra (1.3.2) gem 1.3.7 running the scripts from dos window by `ruby script_name.rb` Few first lines from my requires.rb are below. When I run web.rb I can see the text "loading web requires". Note that I require 'rubygems' for all .rb files. If I comment `require 'sinatra' I get an error message 'web.rb:17: uninitialized constant Rack (NameError)'. If the require is not commented ruby finishes without any message on the screen, no error message either. ``` called_from=caller[0].split(":")[0] puts "loading web 'requires' for file: #{called_from} ..." if (["web"].any?{|s| called_from[s]}) require "c:\\edutester\\others\\settings.rb" require 'rubygems' require 'sinatra' if (["web"].any?{|s| called_from[s]}) ``` UPDATE I simplified the test case. I have two files. If I use only test.rb and require in the file. Everything works fine. If I use test_require.rb file I can see "loading" text and I'd say that something is loading or something is processin because it takes a second or so to finish the test.rb test.rb ``` #require "c:\\edutester\\playground\\test_require.rb" require 'rubygems' require 'sinatra' get '/' do "yes" end ``` and test_require.rb ``` puts "loading..." require 'rubygems' require 'sinatra' ``` Sinatra won't load = I don't get the usual ``` == Sinatra/1.3.2 has taken the stage on 4567 for development with backup from Thin >> Thin web server (v1.2.7 codename No Hup) >> Maximum connections set to 1024 >> Listening on 0.0.0.0:4567, CTRL+C to stop ``` and I cannot access the web server from a browser. And there is no message at all. Hm, there is one empty line

Original source