ruby-changes:48124
From: akr <ko1@a...>
Date: Sat, 21 Oct 2017 17:34:54 +0900 (JST)
Subject: [ruby-changes:48124] akr:r60238 (trunk): Pathname#glob method implemented.
akr 2017-10-21 17:34:49 +0900 (Sat, 21 Oct 2017) New Revision: 60238 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60238 Log: Pathname#glob method implemented. [ruby-core:49373] [Feature #7360] proposed by Alexander E. Fischer. Modified files: trunk/NEWS trunk/ext/pathname/pathname.c trunk/test/pathname/test_pathname.rb Index: NEWS =================================================================== --- NEWS (revision 60237) +++ NEWS (revision 60238) @@ -150,6 +150,10 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L150 environment variable if the system's environment variable is multiuser safe. [Bug #12921] +* Pathname + * New method: + * Pathname#glob [Feature #7360] + * RbConfig * New constants: * RbConfig::LIMITS is added to provide the limits of C types. Index: ext/pathname/pathname.c =================================================================== --- ext/pathname/pathname.c (revision 60237) +++ ext/pathname/pathname.c (revision 60238) @@ -3,6 +3,7 @@ https://github.com/ruby/ruby/blob/trunk/ext/pathname/pathname.c#L3 static VALUE rb_cPathname; static ID id_at_path, id_to_path; +static ID id_base; static ID id_ENOTDIR, id_atime, id_basename, id_binread, id_binwrite, id_birthtime, id_blockdev_p, id_chardev_p, id_chmod, id_chown, id_ctime, id_directory_p, id_dirname, id_empty_p, id_entries, @@ -1021,7 +1022,7 @@ path_empty_p(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/pathname/pathname.c#L1022 } static VALUE -glob_i(RB_BLOCK_CALL_FUNC_ARGLIST(elt, klass)) +s_glob_i(RB_BLOCK_CALL_FUNC_ARGLIST(elt, klass)) { return rb_yield(rb_class_new_instance(1, &elt, klass)); } @@ -1042,7 +1043,7 @@ path_s_glob(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/ext/pathname/pathname.c#L1043 n = rb_scan_args(argc, argv, "11", &args[0], &args[1]); if (rb_block_given_p()) { - return rb_block_call(rb_cDir, id_glob, n, args, glob_i, klass); + return rb_block_call(rb_cDir, id_glob, n, args, s_glob_i, klass); } else { VALUE ary; @@ -1058,6 +1059,54 @@ path_s_glob(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/ext/pathname/pathname.c#L1059 } } +static VALUE +glob_i(RB_BLOCK_CALL_FUNC_ARGLIST(elt, self)) +{ + elt = rb_funcall(self, '+', 1, elt); + return rb_yield(elt); +} + +/* + * Returns or yields Pathname objects. + * + * Pathname("ruby-2.4.2").glob("R*.md") + * #=> [#<Pathname:ruby-2.4.2/README.md>, #<Pathname:ruby-2.4.2/README.ja.md>] + * + * See Dir.glob. + * This method uses base: argument of Dir.glob. + */ +static VALUE +path_glob(int argc, VALUE *argv, VALUE self) +{ + VALUE args[3]; + int n; + + n = rb_scan_args(argc, argv, "11", &args[0], &args[1]); + if (n == 1) + args[1] = INT2FIX(0); + + args[2] = rb_hash_new(); + rb_hash_aset(args[2], ID2SYM(id_base), get_strpath(self)); + + n = 3; + + if (rb_block_given_p()) { + return rb_block_call(rb_cDir, id_glob, n, args, glob_i, self); + } + else { + VALUE ary; + long i; + ary = rb_funcallv(rb_cDir, id_glob, n, args); + ary = rb_convert_type(ary, T_ARRAY, "Array", "to_ary"); + for (i = 0; i < RARRAY_LEN(ary); i++) { + VALUE elt = RARRAY_AREF(ary, i); + elt = rb_funcall(self, '+', 1, elt); + rb_ary_store(ary, i, elt); + } + return ary; + } +} + /* * Returns the current working directory as a Pathname. * @@ -1485,6 +1534,7 @@ Init_pathname(void) https://github.com/ruby/ruby/blob/trunk/ext/pathname/pathname.c#L1534 rb_define_singleton_method(rb_cPathname, "glob", path_s_glob, -1); rb_define_singleton_method(rb_cPathname, "getwd", path_s_getwd, 0); rb_define_singleton_method(rb_cPathname, "pwd", path_s_getwd, 0); + rb_define_method(rb_cPathname, "glob", path_glob, -1); rb_define_method(rb_cPathname, "entries", path_entries, 0); rb_define_method(rb_cPathname, "mkdir", path_mkdir, -1); rb_define_method(rb_cPathname, "rmdir", path_rmdir, 0); @@ -1505,6 +1555,7 @@ InitVM_pathname(void) https://github.com/ruby/ruby/blob/trunk/ext/pathname/pathname.c#L1555 id_ENOTDIR = rb_intern("ENOTDIR"); id_atime = rb_intern("atime"); id_basename = rb_intern("basename"); + id_base = rb_intern("base"); id_binread = rb_intern("binread"); id_binwrite = rb_intern("binwrite"); id_birthtime = rb_intern("birthtime"); Index: test/pathname/test_pathname.rb =================================================================== --- test/pathname/test_pathname.rb (revision 60237) +++ test/pathname/test_pathname.rb (revision 60238) @@ -1250,6 +1250,19 @@ class TestPathname < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/pathname/test_pathname.rb#L1250 assert_kind_of(Pathname, wd) end + def test_glob + with_tmpchdir('rubytest-pathname') {|dir| + Dir.mkdir("d") + open("d/f", "w") {|f| f.write "abc" } + Dir.mkdir("d/e") + assert_equal([Pathname("d/e"), Pathname("d/f")], Pathname("d").glob("*").sort) + a = [] + Pathname("d").glob("*") {|path| a << path } + a.sort! + assert_equal([Pathname("d/e"), Pathname("d/f")], a) + } + end + def test_entries with_tmpchdir('rubytest-pathname') {|dir| open("a", "w") {} -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/