summaryrefslogtreecommitdiff
path: root/lib/pathname.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pathname.rb')
-rw-r--r--lib/pathname.rb16
1 files changed, 15 insertions, 1 deletions
diff --git a/lib/pathname.rb b/lib/pathname.rb
index 8a07d455fd..75faaef8b2 100644
--- a/lib/pathname.rb
+++ b/lib/pathname.rb
@@ -254,7 +254,21 @@ class Pathname
# Return a pathname which is substituted by String#sub.
def sub(pattern, *rest, &block)
- self.class.new(@path.sub(pattern, *rest, &block))
+ if block
+ path = @path.sub(pattern, *rest) {|*args|
+ begin
+ old = Thread.current[:pathname_sub_matchdata]
+ Thread.current[:pathname_sub_matchdata] = $~
+ eval("$~ = Thread.current[:pathname_sub_matchdata]", block.binding)
+ ensure
+ Thread.current[:pathname_sub_matchdata] = old
+ end
+ yield *args
+ }
+ else
+ path = @path.sub(pattern, *rest)
+ end
+ self.class.new(path)
end
if File::ALT_SEPARATOR