ruby-changes:11368
From: akr <ko1@a...>
Date: Tue, 17 Mar 2009 14:57:10 +0900 (JST)
Subject: [ruby-changes:11368] Ruby:r22988 (ruby_1_8): * lib/pathname.rb (Pathname#sub): set $~ in block.binding.
akr 2009-03-17 14:56:59 +0900 (Tue, 17 Mar 2009) New Revision: 22988 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=22988 Log: * lib/pathname.rb (Pathname#sub): set $~ in block.binding. [ruby-dev:38173] Modified files: branches/ruby_1_8/ChangeLog branches/ruby_1_8/lib/pathname.rb branches/ruby_1_8/test/pathname/test_pathname.rb Index: ruby_1_8/ChangeLog =================================================================== --- ruby_1_8/ChangeLog (revision 22987) +++ ruby_1_8/ChangeLog (revision 22988) @@ -1,3 +1,8 @@ +Tue Mar 17 14:56:26 2009 Tanaka Akira <akr@f...> + + * lib/pathname.rb (Pathname#sub): set $~ in block.binding. + [ruby-dev:38173] + Mon Mar 16 16:37:22 2009 Nobuyoshi Nakada <nobu@r...> * ext/tk/tcltklib.c (eventloop_sleep, lib_eventloop_core), Index: ruby_1_8/lib/pathname.rb =================================================================== --- ruby_1_8/lib/pathname.rb (revision 22987) +++ ruby_1_8/lib/pathname.rb (revision 22988) @@ -251,7 +251,21 @@ # 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 Index: ruby_1_8/test/pathname/test_pathname.rb =================================================================== --- ruby_1_8/test/pathname/test_pathname.rb (revision 22987) +++ ruby_1_8/test/pathname/test_pathname.rb (revision 22988) @@ -381,6 +381,15 @@ def pathsub(path, pat, repl) Pathname.new(path).sub(pat, repl).to_s end defassert(:pathsub, "a.o", "a.c", /\.c\z/, ".o") + def test_sub_matchdata + result = Pathname("abc.gif").sub(/\..*/) { + assert_not_nil($~) + assert_equal(".gif", $~[0]) + ".png" + } + assert_equal("abc.png", result.to_s) + end + def root?(path) Pathname.new(path).root? end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/