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

MiniQuark picture MiniQuark · Apr 25, 2009 · Viewed 27.5k times · Source

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! ;-)

Answer

knut picture knut · Oct 22, 2011

Since ruby 1.9 you can use require_relative.