ruby-changes:17246
From: akr <ko1@a...>
Date: Tue, 14 Sep 2010 05:14:12 +0900 (JST)
Subject: [ruby-changes:17246] Ruby:r29247 (trunk): * ext/pathname/pathname.c (path_s_glob): Pathname.glob translated
akr 2010-09-14 05:14:04 +0900 (Tue, 14 Sep 2010) New Revision: 29247 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29247 Log: * ext/pathname/pathname.c (path_s_glob): Pathname.glob translated from pathname.rb. Modified files: trunk/ChangeLog trunk/ext/pathname/lib/pathname.rb trunk/ext/pathname/pathname.c trunk/test/pathname/test_pathname.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 29246) +++ ChangeLog (revision 29247) @@ -1,3 +1,8 @@ +Tue Sep 14 05:13:04 2010 Tanaka Akira <akr@f...> + + * ext/pathname/pathname.c (path_s_glob): Pathname.glob translated + from pathname.rb. + Tue Sep 14 01:24:51 2010 Yutaka Kanemoto <kanemoto@r...> * ext/socket/raddrinfo.c (ruby_getaddrinfo__aix): suppress a Index: ext/pathname/lib/pathname.rb =================================================================== --- ext/pathname/lib/pathname.rb (revision 29246) +++ ext/pathname/lib/pathname.rb (revision 29247) @@ -485,15 +485,6 @@ 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 Index: ext/pathname/pathname.c =================================================================== --- ext/pathname/pathname.c (revision 29246) +++ ext/pathname/pathname.c (revision 29247) @@ -813,7 +813,40 @@ 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 * * Pathname represents a pathname which locates a file in a filesystem. @@ -1066,4 +1099,5 @@ 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); } Index: test/pathname/test_pathname.rb =================================================================== --- test/pathname/test_pathname.rb (revision 29246) +++ test/pathname/test_pathname.rb (revision 29247) @@ -1152,6 +1152,10 @@ 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/