aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-09-13 20:14:04 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-09-13 20:14:04 +0000
commit6d9b5fdffcfe2fc2783c5b03f93e2deb372fd260 (patch)
tree24f93de60715359fee8da2b18b37bb2fd3aa5c84
parent1428b74ff9acdfb435b8bddf3d19e3b50868dfd8 (diff)
downloadruby-6d9b5fdffcfe2fc2783c5b03f93e2deb372fd260.tar.gz
* ext/pathname/pathname.c (path_s_glob): Pathname.glob translated
from pathname.rb. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29247 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--ext/pathname/lib/pathname.rb9
-rw-r--r--ext/pathname/pathname.c34
-rw-r--r--test/pathname/test_pathname.rb4
4 files changed, 43 insertions, 9 deletions
diff --git a/ChangeLog b/ChangeLog
index 152ad085f4..3979eff508 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Tue Sep 14 05:13:04 2010 Tanaka Akira <akr@fsij.org>
+
+ * ext/pathname/pathname.c (path_s_glob): Pathname.glob translated
+ from pathname.rb.
+
Tue Sep 14 01:24:51 2010 Yutaka Kanemoto <kanemoto@ruby-lang.org>
* ext/socket/raddrinfo.c (ruby_getaddrinfo__aix): suppress a
diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb
index 6cf6419e52..e519dd56d2 100644
--- a/ext/pathname/lib/pathname.rb
+++ b/ext/pathname/lib/pathname.rb
@@ -485,15 +485,6 @@ end
class Pathname # * Dir *
- # See <tt>Dir.glob</tt>. Returns or yields Pathname objects.
- def Pathname.glob(*args) # :yield: pathname
- if block_given?
- Dir.glob(*args) {|f| yield self.new(f) }
- else
- Dir.glob(*args).map {|f| self.new(f) }
- end
- end
-
# See <tt>Dir.getwd</tt>. Returns the current working directory as a Pathname.
def Pathname.getwd() self.new(Dir.getwd) end
class << self; alias pwd getwd end
diff --git a/ext/pathname/pathname.c b/ext/pathname/pathname.c
index d1090f841c..eca94b4d16 100644
--- a/ext/pathname/pathname.c
+++ b/ext/pathname/pathname.c
@@ -813,6 +813,39 @@ path_zero_p(VALUE self)
return rb_funcall(rb_mFileTest, rb_intern("zero?"), 1, get_strpath(self));
}
+static VALUE
+glob_i(VALUE elt, VALUE klass, int argc, VALUE *argv)
+{
+ return rb_yield(rb_class_new_instance(1, &elt, klass));
+}
+
+/*
+ * See <tt>Dir.glob</tt>. Returns or yields Pathname objects.
+ */
+static VALUE
+path_s_glob(int argc, VALUE *argv, VALUE klass)
+{
+ VALUE args[2];
+ int n;
+
+ n = rb_scan_args(argc, argv, "11", &args[0], &args[1]);
+ if (rb_block_given_p()) {
+ return rb_block_call(rb_cDir, rb_intern("glob"), n, args, glob_i, klass);
+ }
+ else {
+ VALUE ary;
+ long i;
+ ary = rb_funcall2(rb_cDir, rb_intern("glob"), n, args);
+ ary = rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
+ for (i = 0; i < RARRAY_LEN(ary); i++) {
+ VALUE elt = RARRAY_PTR(ary)[i];
+ elt = rb_class_new_instance(1, &elt, klass);
+ rb_ary_store(ary, i, elt);
+ }
+ return ary;
+ }
+}
+
/*
* == Pathname
*
@@ -1066,4 +1099,5 @@ Init_pathname()
rb_define_method(rb_cPathname, "world_writable?", path_world_writable_p, 0);
rb_define_method(rb_cPathname, "writable_real?", path_writable_real_p, 0);
rb_define_method(rb_cPathname, "zero?", path_zero_p, 0);
+ rb_define_singleton_method(rb_cPathname, "glob", path_s_glob, -1);
}
diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
index 55ef46690d..340af2423d 100644
--- a/test/pathname/test_pathname.rb
+++ b/test/pathname/test_pathname.rb
@@ -1152,6 +1152,10 @@ class TestPathname < Test::Unit::TestCase
open("f", "w") {|f| f.write "abc" }
Dir.mkdir("d")
assert_equal([Pathname("d"), Pathname("f")], Pathname.glob("*").sort)
+ a = []
+ Pathname.glob("*") {|path| a << path }
+ a.sort!
+ assert_equal([Pathname("d"), Pathname("f")], a)
}
end