21

I have a ruby file that does not have a .rb extension, and is instead identified as ruby code with a shebang at the beginning of the file: #!/usr/bin/env ruby. I want to require the code in this file in another ruby file, but it seems to be having a problem because require automatically appends the .rb extension to the files it looks for. Is there any way to suppress this behavior and make require only look for the file as the name is given?

Kvass
  • 8,294
  • 12
  • 65
  • 108

3 Answers3

21

Use load instead:

load 'file-name'
NARKOZ
  • 27,203
  • 7
  • 68
  • 90
  • 9
    Be aware of the differences in behavior between `load` and `require`. They aren't exactly replacements for each other. – the Tin Man Dec 06 '13 at 13:58
  • 8
    One notable difference: `require` loads the file only once, `load` does it every time. Docs: http://ruby-doc.org/core-2.2.2/Kernel.html#method-i-load – Jared Beck May 09 '15 at 20:07
  • nice article describing the differences: https://medium.com/@connorstack/understanding-ruby-load-require-gems-bundler-and-rails-autoloading-from-the-bottom-up-3b422902ca0 – John Bachir Dec 30 '20 at 17:40
3

We have scripts the users are allowed to call, which are accessed using symlinks. The symlinks exist in our /usr/local/bin directory, which is automatically included in their path via a minor adjustment to /etc/profile.

Our real scripts exist in a separate directory using the normal Ruby name, ending with ".rb". We can access those using require if necessary.

/usr/local/bin/foo --> /usr/local/share/app_name/foo.rb
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
0

Add a symlink to the file and then require that.

ln -s bin/foo lib/stubs/foo.rb
require 'stubs/foo'
grosser
  • 14,707
  • 7
  • 57
  • 61