Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adding support for require_relative. #1184

Merged
merged 1 commit into from

3 participants

@steveklabnik
Collaborator

Done the right way this time.

@steveklabnik steveklabnik Adding support for require_relative.
This is based on my require_relative gem for MRI. Passes
all of the RubySpec specs.
daff704
@brixen brixen merged commit daff704 into rubinius:master
@parndt

<3 :+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 4, 2011
  1. @steveklabnik

    Adding support for require_relative.

    steveklabnik authored
    This is based on my require_relative gem for MRI. Passes
    all of the RubySpec specs.
This page is out of date. Refresh to see the latest.
Showing with 22 additions and 2 deletions.
  1. +11 −2 kernel/common/codeloader19.rb
  2. +11 −0 kernel/common/kernel19.rb
View
13 kernel/common/codeloader19.rb
@@ -11,9 +11,18 @@ def search_load_path(name, loading)
return path if loadable? path
end
- return name if loading and loadable? "./#{name}"
-
return nil
end
+
+ # requires files relative to the current directory. We do one interesting
+ # check to make sure it's not called inside of an eval.
+ def self.require_relative(name, scope)
+ script = scope.current_script
+ if script
+ require File.expand_path(name, File.dirname(script.data_path))
+ else
+ raise LoadError.new "Something is wrong in trying to get relative path"
+ end
+ end
end
end
View
11 kernel/common/kernel19.rb
@@ -85,4 +85,15 @@ def proc(&prc)
end
module_function :proc
+
+ # Attempt to load the given file, returning true if successful. Works just
+ # like Kernel#require, except that it searches relative to the current
+ # directory.
+ #
+ def require_relative(name)
+ scope = Rubinius::StaticScope.of_sender
+ Rubinius::CodeLoader.require_relative(name, scope)
+ end
+ module_function :require_relative
+
end
Something went wrong with that request. Please try again.