Top Level Namespace

Defined Under Namespace

Modules: RequireRelative

Instance Method Summary collapse

Instance Method Details

#require_relative(relative_feature) ⇒ Object

Yep, you’re looking at it! This gem is pretty small, and for good reason. There’s not much to do! We use split to find the filename that we’re looking to require, raise a LoadError if it’s called in a context (like eval) that it shouldn’t be, and then require it via regular old require.

Now, in 1.9, “.” is totally removed from the $LOAD_PATH. We don’t do that here, because that would break a lot of other code! You’re still vulnerable to the security hole that caused this change to happen in the first place. You will be able to use this gem to transition the code you write over to the 1.9 syntax, though.

Raises:

  • (LoadError)


22
23
24
25
26
27
28
29
# File 'lib/require_relative.rb', line 22

def require_relative(relative_feature)

  file = caller.first.split(/:\d/,2).first

  raise LoadError, "require_relative is called in #{$1}" if /\A\((.*)\)/ =~ file

  require File.expand_path(relative_feature, File.dirname(file))
end