Skip to content
This repository

Adding support for require_relative. #1184

Merged
merged 1 commit into from over 2 years ago

3 participants

Steve Klabnik Philip Arndt Brian Shirai
Steve Klabnik
Collaborator

Done the right way this time.

Steve Klabnik Adding support for require_relative.
This is based on my require_relative gem for MRI. Passes
all of the RubySpec specs.
daff704
Brian Shirai brixen merged commit daff704 into from October 03, 2011
Brian Shirai brixen closed this October 03, 2011
Philip Arndt

<3 :+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Oct 03, 2011
Steve Klabnik Adding support for require_relative.
This is based on my require_relative gem for MRI. Passes
all of the RubySpec specs.
daff704
This page is out of date. Refresh to see the latest.
13  kernel/common/codeloader19.rb
@@ -11,9 +11,18 @@ def search_load_path(name, loading)
11 11
         return path if loadable? path
12 12
       end
13 13
 
14  
-      return name if loading and loadable? "./#{name}"
15  
-
16 14
       return nil
17 15
     end
  16
+
  17
+    # requires files relative to the current directory. We do one interesting
  18
+    # check to make sure it's not called inside of an eval.
  19
+    def self.require_relative(name, scope)
  20
+      script = scope.current_script
  21
+      if script
  22
+        require File.expand_path(name, File.dirname(script.data_path))
  23
+      else
  24
+        raise LoadError.new "Something is wrong in trying to get relative path"
  25
+      end
  26
+    end
18 27
   end
19 28
 end
11  kernel/common/kernel19.rb
@@ -85,4 +85,15 @@ def proc(&prc)
85 85
   end
86 86
 
87 87
   module_function :proc
  88
+
  89
+  # Attempt to load the given file, returning true if successful. Works just
  90
+  # like Kernel#require, except that it searches relative to the current
  91
+  # directory.
  92
+  #
  93
+  def require_relative(name)
  94
+    scope = Rubinius::StaticScope.of_sender
  95
+    Rubinius::CodeLoader.require_relative(name, scope)
  96
+  end
  97
+  module_function :require_relative
  98
+
88 99
 end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.