Is there a shorter way to require a file in the same directory in ruby?

RubyPathRequire

Ruby Problem Overview


Is there a shorter way to require a file located in the same directory (as the script being executed)?

require File.expand_path(File.dirname(__FILE__) + '/some_other_script')

I read that require "my_script" and require "./my_script" will actually load the script twice (ruby will not recognize that it is actually the same script), and this is the reason why File.expand_path is recommended: if it is used every time the script is required, then it will only be loaded once.

It seems weird to me that a concise language like Ruby does not seem to have a shorter solution. For example, python simply has this:

import .some_other_module_in_the_same_directory

I guess I could monkey-patch require... but that's just evil! ;-)

Ruby Solutions


Solution 1 - Ruby

Since ruby 1.9 you can use require_relative.

Check the latest doc for require_relative or another version of the Core API.

Solution 2 - Ruby

Just require filename.

Yes, it will import it twice if you specify it as filename and ./filename, so don't do that. You're not specifying the .rb, so don't specify the path. I usually put the bulk of my application logic into a file in lib, and then have a script in bin that looks something like this:

#!/usr/bin/env ruby

$: << File.join(File.dirname(__FILE__), "/../lib")
require 'app.rb'
App.new.run(ARGV)

Another advantage is that I find it easier to do unit testing if the loading the application logic doesn't automatically start executing it.

Solution 3 - Ruby

The above will work even when you're running the script from some other directory. However, inside the same directory the shorter forms you refer to work as expected and at least for ruby 1.9 won't result in a double-require.

#testa.rb

puts "start test A"
require 'testb'
require './testb'
puts "finish test A"

#testb.rb

puts "start test B"
puts "finish test B"

running 'ruby testa.rb' will result in:

start test A
start test B
finish test B
finish test A

However, the longer form will work even from another directory (eg. ruby somedir/script.rb)

Solution 4 - Ruby

Put this in a standard library directory (somewhere that's already in your default loadpath $:):

# push-loadpath.rb
if caller.first
  $: << File.expand_path(File.dirname(caller.first))
end

Then, this should work

% ls /path/to/
bin.rb lib1.rb lib2.rb #...
% cat /path/to/bin.rb
load 'push-loadpath.rb'
require 'lib1'
require 'lib2'
#...

caller gives you access to the current callstack, and tells you what file and where, so push-loadpath.rb uses that to add the file that load'd it to the loadpath.

Note that you should load the file, rather than require it, so the body can be invoked multiple times (once for each time you want to alter the loadpath).

Alternately, you could wrap the body in a method,

# push-loadpath.rb
def push_loadpath
  $: << File.expand_path(File.dirname(caller.first))
end

This would allow you to require it, and use it this way:

% ls /path/to/
bin.rb lib1.rb lib2.rb #...
% cat /path/to/bin.rb
require 'push-loadpath'
push_loadpath
require 'lib1'
require 'lib2'
#...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMiniQuarkView Question on Stackoverflow
Solution 1 - RubyknutView Answer on Stackoverflow
Solution 2 - RubydvorakView Answer on Stackoverflow
Solution 3 - RubywoodsView Answer on Stackoverflow
Solution 4 - RubyrampionView Answer on Stackoverflow