Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Toy #2

Open
wants to merge 2 commits into from

2 participants

Ivan Urabe, Shyouhei
Ivan

Fixed Pathname + and relative_path_from methods: they where returning instance of Pathname even if called for instance of Pathname subclass.

added some commits December 02, 2010
Ivan fixed Pathname + method: if subclassing Pathname, when using + method…
… on instance of that subclass, method returns instance of Pathname, not instance of subclass
551d93a
Ivan fixed Pathname relative_path_from method: if subclassing Pathname, wh…
…en using relative_path_from method on instance of that subclass, method returns instance of Pathname, not instance of subclass
beea741
Urabe, Shyouhei
Owner

Akr, the author of Pathname class, said me that when you inherit the Array class,

class A < Array
end
p((A.new + A.new).class)

outputs Array instead of A. He thinks current Pathname behaviour is consistent with it.

Ivan
toy commented May 09, 2011

Array methods always return instance of Array, but all Pathname methods (for example split, ascend, dirname, class methods glob, pwd) except + and relative_path_from returns instances of inheriting class, so it is not full consistency.

Ivan

I still got no reply for this pull request.

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

Showing 2 unique commits by 1 author.

Dec 02, 2010
Ivan fixed Pathname + method: if subclassing Pathname, when using + method…
… on instance of that subclass, method returns instance of Pathname, not instance of subclass
551d93a
Ivan fixed Pathname relative_path_from method: if subclassing Pathname, wh…
…en using relative_path_from method on instance of that subclass, method returns instance of Pathname, not instance of subclass
beea741
This page is out of date. Refresh to see the latest.
8  ext/pathname/lib/pathname.rb
@@ -305,8 +305,8 @@ def ascend
305 305
   # This method doesn't access the file system; it is pure string manipulation.
306 306
   #
307 307
   def +(other)
308  
-    other = Pathname.new(other) unless Pathname === other
309  
-    Pathname.new(plus(@path, other.to_s))
  308
+    other = self.class.new(other) unless Pathname === other
  309
+    self.class.new(plus(@path, other.to_s))
310 310
   end
311 311
 
312 312
   def plus(path1, path2) # -> path
@@ -476,9 +476,9 @@ def relative_path_from(base_directory)
476 476
     base_names.fill('..')
477 477
     relpath_names = base_names + dest_names
478 478
     if relpath_names.empty?
479  
-      Pathname.new('.')
  479
+      self.class.new('.')
480 480
     else
481  
-      Pathname.new(File.join(*relpath_names))
  481
+      self.class.new(File.join(*relpath_names))
482 482
     end
483 483
   end
484 484
 end
11  test/pathname/test_pathname.rb
@@ -30,6 +30,9 @@ def self.defassert(name, result, *args)
30 30
   DOSISH_DRIVE_LETTER = File.dirname("A:") == "A:."
31 31
   DOSISH_UNC = File.dirname("//") == "//"
32 32
 
  33
+  class SuperPathname < Pathname
  34
+  end
  35
+
33 36
   def cleanpath_aggressive(path)
34 37
     Pathname.new(path).cleanpath.to_s
35 38
   end
@@ -204,6 +207,10 @@ def plus(path1, path2) # -> path
204 207
 
205 208
   defassert(:plus, 'a//b/d//e', 'a//b/c', '../d//e')
206 209
 
  210
+  define_assertion(:plus) {
  211
+    assert_instance_of(SuperPathname, SuperPathname.new('a') + 'b')
  212
+  }
  213
+
207 214
   def test_parent
208 215
     assert_equal(Pathname("."), Pathname("a").parent)
209 216
   end
@@ -306,6 +313,10 @@ def self.defassert_raise(name, exc, *args)
306 313
   defassert_raise(:relative_path_from, ArgumentError, "a", "..")
307 314
   defassert_raise(:relative_path_from, ArgumentError, ".", "..")
308 315
 
  316
+  define_assertion(:relative_path_from) {
  317
+    assert_instance_of(SuperPathname, SuperPathname.new('a').relative_path_from(SuperPathname.new('b')))
  318
+  }
  319
+
309 320
   def with_tmpchdir(base=nil)
310 321
     Dir.mktmpdir(base) {|d|
311 322
       d = Pathname.new(d).realpath.to_s
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.