Skip to content

Commit f2b250c

Browse files
committed
[ruby/pathname] Define private method same_paths? for Ractor-safety
ruby/pathname@d33d18e5e2
1 parent 4080abe commit f2b250c

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

pathname_builtin.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,12 @@ class Pathname
195195

196196
# :stopdoc:
197197

198-
SAME_PATHS = if File::FNM_SYSCASE.nonzero?
198+
if File::FNM_SYSCASE.nonzero?
199199
# Avoid #zero? here because #casecmp can return nil.
200-
proc {|a, b| a.casecmp(b) == 0}
200+
private def same_paths?(a, b) a.casecmp(b) == 0 end
201201
else
202-
proc {|a, b| a == b}
202+
private def same_paths?(a, b) a == b end
203203
end
204-
SAME_PATHS.freeze
205-
private_constant :SAME_PATHS
206204

207205
attr_reader :path
208206
protected :path
@@ -836,12 +834,12 @@ def relative_path_from(base_directory)
836834
base_prefix, basename = r
837835
base_names.unshift basename if basename != '.'
838836
end
839-
unless SAME_PATHS[dest_prefix, base_prefix]
837+
unless same_paths?(dest_prefix, base_prefix)
840838
raise ArgumentError, "different prefix: #{dest_prefix.inspect} and #{base_directory.inspect}"
841839
end
842840
while !dest_names.empty? &&
843841
!base_names.empty? &&
844-
SAME_PATHS[dest_names.first, base_names.first]
842+
same_paths?(dest_names.first, base_names.first)
845843
dest_names.shift
846844
base_names.shift
847845
end

test/pathname/test_ractor.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class Ractor
2020
x.join(Pathname("b"), Pathname("c"))
2121
end
2222
assert_equal(Pathname("a/b/c"), r.value)
23+
24+
r = Ractor.new Pathname("a") do |a|
25+
Pathname("b").relative_path_from(a)
26+
end
27+
assert_equal(Pathname("../b"), r.value)
2328
end;
2429
end
2530
end

0 commit comments

Comments
 (0)