Skip to content

Commit 5667704

Browse files
eregonjacob-shops
authored andcommitted
Support passing a #to_str object to Pathname.new for compatibility
* See ruby/pathname#57 (comment)
1 parent 6b94472 commit 5667704

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

pathname_builtin.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ class Pathname
217217
def initialize(path)
218218
unless String === path
219219
path = path.to_path if path.respond_to? :to_path
220-
raise TypeError unless String === path
220+
path = path.to_str if path.respond_to? :to_str
221+
raise TypeError, "Pathname.new requires a String, #to_path or #to_str" unless String === path
221222
end
222223

223224
if path.include?("\0")

test/pathname/test_pathname.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,10 @@ def test_initialize
484484
assert_equal('a', p1.to_s)
485485
p2 = Pathname.new(p1)
486486
assert_equal(p1, p2)
487+
488+
obj = Object.new
489+
def obj.to_str; "a/b"; end
490+
assert_equal("a/b", Pathname.new(obj).to_s)
487491
end
488492

489493
def test_initialize_nul

0 commit comments

Comments
 (0)