1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
yugui 2009-04-19 13:33:18 +00:00
parent 3867806101
commit 22bdfe1413
3 changed files with 29 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -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