Permalink
Browse files

require_relative should have no effect on 1.9

There are libraries that use #require_relative w/o adding it to dependencies.
In order for them to completely safely depend on require_relative gem, it
should have no effect on 1.9.
  • Loading branch information...
1 parent a1b8f7e commit a5c0b00cc2226023261adc85ac31802cf2d98eb3 @michaelklishin michaelklishin committed Jun 18, 2011
Showing with 18 additions and 14 deletions.
  1. +18 −14 lib/require_relative.rb
View
@@ -6,21 +6,25 @@
# for more information on how to build your own copy of the gem, as well as
# contribute fixes.
-# 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.
-def require_relative(relative_feature)
+# require_relative has no effect on Ruby 1.9 (or other versions that provide Kernel#require_relative
+# out of the box)
+unless Object.new.respond_to?(:require_relative, true)
+ # 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.
+ def require_relative(relative_feature)
- file = caller.first.split(/:\d/,2).first
+ file = caller.first.split(/:\d/,2).first
- raise LoadError, "require_relative is called in #{$1}" if /\A\((.*)\)/ =~ file
+ raise LoadError, "require_relative is called in #{$1}" if /\A\((.*)\)/ =~ file
- require File.expand_path(relative_feature, File.dirname(file))
+ require File.expand_path(relative_feature, File.dirname(file))
+ end
end

0 comments on commit a5c0b00

Please sign in to comment.