From 22bdfe14138a950765a8fc66c149a3f840cd5424 Mon Sep 17 00:00:00 2001 From: yugui Date: Sun, 19 Apr 2009 13:33:18 +0000 Subject: merges r22987 from trunk into ruby_1_9_1. -- * lib/pathname.rb (Pathname#sub): set $~ in block.binding. [ruby-dev:38173] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_1@23217 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/pathname.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pathname.rb b/lib/pathname.rb index 86f0f54800..ed98c7b367 100644 --- a/lib/pathname.rb +++ b/lib/pathname.rb @@ -251,7 +251,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 -- cgit v1.2.3