diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb index 224c869f67172f..47a657dc666333 100644 --- a/ext/pathname/lib/pathname.rb +++ b/ext/pathname/lib/pathname.rb @@ -305,8 +305,8 @@ def ascend # This method doesn't access the file system; it is pure string manipulation. # def +(other) - other = Pathname.new(other) unless Pathname === other - Pathname.new(plus(@path, other.to_s)) + other = self.class.new(other) unless Pathname === other + self.class.new(plus(@path, other.to_s)) end def plus(path1, path2) # -> path @@ -476,9 +476,9 @@ def relative_path_from(base_directory) base_names.fill('..') relpath_names = base_names + dest_names if relpath_names.empty? - Pathname.new('.') + self.class.new('.') else - Pathname.new(File.join(*relpath_names)) + self.class.new(File.join(*relpath_names)) end end end diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb index 86b85b894a2c75..3c59a747a66831 100644 --- a/test/pathname/test_pathname.rb +++ b/test/pathname/test_pathname.rb @@ -30,6 +30,9 @@ def self.defassert(name, result, *args) DOSISH_DRIVE_LETTER = File.dirname("A:") == "A:." DOSISH_UNC = File.dirname("//") == "//" + class SuperPathname < Pathname + end + def cleanpath_aggressive(path) Pathname.new(path).cleanpath.to_s end @@ -204,6 +207,10 @@ def plus(path1, path2) # -> path defassert(:plus, 'a//b/d//e', 'a//b/c', '../d//e') + define_assertion(:plus) { + assert_instance_of(SuperPathname, SuperPathname.new('a') + 'b') + } + def test_parent assert_equal(Pathname("."), Pathname("a").parent) end @@ -306,6 +313,10 @@ def self.defassert_raise(name, exc, *args) defassert_raise(:relative_path_from, ArgumentError, "a", "..") defassert_raise(:relative_path_from, ArgumentError, ".", "..") + define_assertion(:relative_path_from) { + assert_instance_of(SuperPathname, SuperPathname.new('a').relative_path_from(SuperPathname.new('b'))) + } + def with_tmpchdir(base=nil) Dir.mktmpdir(base) {|d| d = Pathname.new(d).realpath.to_s