summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 0fa194932e..3fd888c57a 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]
+
Sun Mar 15 02:09:31 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* proc.c (bmcall): should not uninitialized variable. a patch from
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
diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
index da12383bdd..c2ce292deb 100644
--- a/test/pathname/test_pathname.rb
+++ b/test/pathname/test_pathname.rb
@@ -392,6 +392,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