summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-17 05:26:15 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-17 05:26:15 +0000
commita2fe450b63bb1d45eeb41c3fa50388d1101aa551 (patch)
tree8e8690ef4920c1981fc7749a2f146a9c4f4b4bc4
parent87ff6d70778173ee1d03582162e65bcaf0164f4e (diff)
downloadruby-a2fe450b63bb1d45eeb41c3fa50388d1101aa551.tar.gz
ruby-a2fe450b63bb1d45eeb41c3fa50388d1101aa551.tar.xz
ruby-a2fe450b63bb1d45eeb41c3fa50388d1101aa551.zip
* lib/pathname.rb (Pathname#sub): set $~ in block.binding.
[ruby-dev:38173] git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@22987 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--lib/pathname.rb16
-rw-r--r--test/pathname/test_pathname.rb9
3 files changed, 29 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 53622a677..cac116f62 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Tue Mar 17 14:25:16 2009 Tanaka Akira <akr@fsij.org>
+
+ * lib/pathname.rb (Pathname#sub): set $~ in block.binding.
+ [ruby-dev:38173]
+
Tue Mar 17 13:48:08 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* win32/Makefile.sub (config.h): added RUBY_COREDLL.
diff --git a/lib/pathname.rb b/lib/pathname.rb
index 8a07d455f..75faaef8b 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
diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
index d979ff702..a9f714963 100644
--- a/test/pathname/test_pathname.rb
+++ b/test/pathname/test_pathname.rb
@@ -471,6 +471,15 @@ class TestPathname < Test::Unit::TestCase
defassert(:pathsubext, 'fooaa.o', 'fooaa', '.o')
defassert(:pathsubext, 'd.e/aa.o', 'd.e/aa', '.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