2

I want to require a file called config.rb in a different ruby file called basics.rb. I'm using Sinatra as my web framework. I'm sure there's a way to do this, I just can't find anything in the docs.

Hopefully it would look something like

post '/' do
  require 'config.rb'
  // logic
end
Ry-
  • 218,210
  • 55
  • 464
  • 476
OneChillDude
  • 7,856
  • 10
  • 40
  • 79
  • What directories (relative to the app root) are these in, and how are you running the app? (This info is necessary to understand how your `$LOAD_PATH` is set, which determines how you can require the files) – Joshua Cheek Aug 25 '12 at 04:46

1 Answers1

3

If config.rb is in your load path, you can require it at the top of your basics.rb file with require 'config'. If it is not in your load path, you'll need something like require '/path/to/your/config'.

The code you've posted will require the file. But only when someone POSTs to '/'.

Also, it's normal to omit the .rb extension when requiring ruby files. But you can include it if you like.

You can view your load path by inspecting the global variable $LOAD_PATH. From the command line ruby -e 'puts $LOAD_PATH' will print it for your version of ruby. You can also add directories to your load path.

Community
  • 1
  • 1
Gordon Wilson
  • 26,244
  • 11
  • 57
  • 60