From 30b0fe4cc2dc3489c1105fe41050c0116186e61a Mon Sep 17 00:00:00 2001 From: shyouhei Date: Wed, 1 Jul 2009 06:48:23 +0000 Subject: merge revision(s) 22988: * 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_8_7@23920 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ lib/pathname.rb | 16 +++++++++++++++- test/pathname/test_pathname.rb | 9 +++++++++ version.h | 11 ++++++----- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index b687051d3d..5b77e9f13f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Wed Jul 1 15:46:30 2009 Tanaka Akira + + * lib/pathname.rb (Pathname#sub): set $~ in block.binding. + [ruby-dev:38173] + Mon Jun 29 13:18:42 2009 Yukihiro Matsumoto * lib/fileutils.rb (FileUtils#fu_get_gid): stringify group diff --git a/lib/pathname.rb b/lib/pathname.rb index e4ca5489ce..86c4b98ee7 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 diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb index 9b0b9c01e8..f94c123dec 100644 --- a/test/pathname/test_pathname.rb +++ b/test/pathname/test_pathname.rb @@ -381,6 +381,15 @@ class TestPathname < Test::Unit::TestCase 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 diff --git a/version.h b/version.h index 69b80d3e49..fa8604fc2a 100644 --- a/version.h +++ b/version.h @@ -1,15 +1,15 @@ #define RUBY_VERSION "1.8.7" -#define RUBY_RELEASE_DATE "2009-06-29" +#define RUBY_RELEASE_DATE "2009-07-01" #define RUBY_VERSION_CODE 187 -#define RUBY_RELEASE_CODE 20090629 -#define RUBY_PATCHLEVEL 175 +#define RUBY_RELEASE_CODE 20090701 +#define RUBY_PATCHLEVEL 176 #define RUBY_VERSION_MAJOR 1 #define RUBY_VERSION_MINOR 8 #define RUBY_VERSION_TEENY 7 #define RUBY_RELEASE_YEAR 2009 -#define RUBY_RELEASE_MONTH 6 -#define RUBY_RELEASE_DAY 29 +#define RUBY_RELEASE_MONTH 7 +#define RUBY_RELEASE_DAY 1 #ifdef RUBY_EXTERN RUBY_EXTERN const char ruby_version[]; @@ -29,3 +29,4 @@ RUBY_EXTERN const char *ruby_copyright; #define RUBY_RELEASE_NUM RUBY_PATCHLEVEL + -- cgit v1.2.3