Support passing a #to_str object to Pathname.new for compatibility

* See https://github.com/ruby/pathname/pull/57#issuecomment-3485646510
This commit is contained in:
Benoit Daloze authored and Hiroshi SHIBATA committed 2025-11-05 14:52:58 +09:00
1 parent f8e9bccd03
commit f8d1291162
2 files changed
+6 -1

No files matched your search

+2 -1
View File
@@ -217,7 +217,8 @@ class Pathname
def initialize(path)
unless String === path
path = path.to_path if path.respond_to? :to_path
raise TypeError unless String === path
path = path.to_str if path.respond_to? :to_str
raise TypeError, "Pathname.new requires a String, #to_path or #to_str" unless String === path
end
if path.include?("\0")
+4
View File
@@ -484,6 +484,10 @@ class TestPathname < Test::Unit::TestCase
assert_equal('a', p1.to_s)
p2 = Pathname.new(p1)
assert_equal(p1, p2)
obj = Object.new
def obj.to_str; "a/b"; end
assert_equal("a/b", Pathname.new(obj).to_s)
end
def test_initialize_nul