Skip to content

Commit 524724e

Browse files
committed
Support passing a #to_str object to Pathname.new for compatibility
* See ruby#57 (comment)
1 parent 4689b0b commit 524724e

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

lib/pathname.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ class Pathname
237237
def initialize(path)
238238
unless String === path
239239
path = path.to_path if path.respond_to? :to_path
240-
raise TypeError unless String === path
240+
path = path.to_str if path.respond_to? :to_str
241+
raise TypeError, "Pathname.new requires a String, #to_path or #to_str" unless String === path
241242
end
242243

243244
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)