aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ext/pathname/lib/pathname.rb8
-rw-r--r--test/pathname/test_pathname.rb14
2 files changed, 20 insertions, 2 deletions
diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb
index 7fb923c6ae..383d47b52f 100644
--- a/ext/pathname/lib/pathname.rb
+++ b/ext/pathname/lib/pathname.rb
@@ -503,9 +503,13 @@ class Pathname
# ArgumentError is raised when it cannot find a relative path.
#
def relative_path_from(base_directory)
- base_directory = Pathname.new(base_directory) unless Pathname === base_directory
dest_directory = self.cleanpath.to_s
- base_directory = base_directory.cleanpath.to_s
+ base_directory =
+ if base_directory.respond_to? :cleanpath
+ base_directory
+ else
+ Pathname.new(base_directory)
+ end.cleanpath.to_s
dest_prefix = dest_directory
dest_names = []
while r = chop_basename(dest_prefix)
diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
index 3bea182ed4..fbf4bb3459 100644
--- a/test/pathname/test_pathname.rb
+++ b/test/pathname/test_pathname.rb
@@ -1429,4 +1429,18 @@ class TestPathname < Test::Unit::TestCase
assert_instance_of(Pathname, foo.relative_path_from(bar))
end;
end
+
+ def test_relative_path_from_mock
+ assert_equal(
+ Pathname.new("../bar"),
+ Pathname.new("/foo/bar").relative_path_from(Pathname.new("/foo/baz")))
+ assert_equal(
+ Pathname.new("../bar"),
+ Pathname.new("/foo/bar").relative_path_from("/foo/baz"))
+ obj = Object.new
+ def obj.cleanpath() Pathname.new("/foo/baz") end
+ assert_equal(
+ Pathname.new("../bar"),
+ Pathname.new("/foo/bar").relative_path_from(obj))
+ end
end