summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-08-01 00:22:18 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-08-01 00:22:18 +0000
commit572b8b37498a875382220d957d1b4285ae046eaf (patch)
treec99094d16c078b18e01e397fa56a763adb6486bd /ext
parent933e5e091106a83dd8f7c402e02703c183f644ee (diff)
* ext/pathname/pathname.c (path_sub): Pathname#sub translated
from pathname.rb. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28812 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r--ext/pathname/lib/pathname.rb19
-rw-r--r--ext/pathname/pathname.c18
2 files changed, 18 insertions, 19 deletions
diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb
index 88453c3c66..b0639a4079 100644
--- a/ext/pathname/lib/pathname.rb
+++ b/ext/pathname/lib/pathname.rb
@@ -31,25 +31,6 @@ class Pathname
# :startdoc:
- # Return a pathname which is substituted by String#sub.
- def 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
SEPARATOR_LIST = "#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}"
SEPARATOR_PAT = /[#{SEPARATOR_LIST}]/
diff --git a/ext/pathname/pathname.c b/ext/pathname/pathname.c
index 5c3620e2b5..37fc87b500 100644
--- a/ext/pathname/pathname.c
+++ b/ext/pathname/pathname.c
@@ -151,6 +151,23 @@ path_inspect(VALUE self)
}
/*
+ * Return a pathname which is substituted by String#sub.
+ */
+static VALUE
+path_sub(int argc, VALUE *argv, VALUE self)
+{
+ VALUE str = get_strpath(self);
+
+ if (rb_block_given_p()) {
+ str = rb_block_call(str, rb_intern("sub"), argc, argv, 0, 0);
+ }
+ else {
+ str = rb_funcall2(str, rb_intern("sub"), argc, argv);
+ }
+ return rb_class_new_instance(1, &str, rb_obj_class(self));
+}
+
+/*
* == Pathname
*
* Pathname represents a pathname which locates a file in a filesystem.
@@ -346,4 +363,5 @@ Init_pathname()
rb_define_method(rb_cPathname, "to_s", path_to_s, 0);
rb_define_method(rb_cPathname, "to_path", path_to_s, 0);
rb_define_method(rb_cPathname, "inspect", path_inspect, 0);
+ rb_define_method(rb_cPathname, "sub", path_sub, -1);
}