Project

General

Profile

Feature #12010

Updated by naruse (Yui NARUSE) over 4 years ago

Dir#each and Dir#read) (including Dir.entries, Dir.foreach and other methods) return "." and ".." at first. 
 But through the all real use case "." and ".." are useless. 
 How about excluding them? 

 ```diff 
 diff --git a/dir.c b/dir.c 
 index 193b5be..4c23a2d 100644 
 --- a/dir.c 
 +++ b/dir.c 
 @@ -699,6 +699,8 @@ fundamental_encoding_p(rb_encoding *enc) 
  #else 
  # define READDIR(dir, enc) readdir((dir)) 
  #endif 
 +#define DIR_IS_DOT_OR_DOTDOT(dp) ((dp)->d_name[0] == '.' && \ 
 + 	 ((dp)->d_name[1] == '\0' || ((dp)->d_name[1] == '.' && (dp)->d_name[2] == '\0'))) 
 
  /* 
   *    call-seq: 
 @@ -720,13 +722,12 @@ dir_read(VALUE dir) 
 
      GetDIR(dir, dirp); 
      errno = 0; 
 -      if ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) { 
 - 	 return rb_external_str_new_with_enc(dp->d_name, NAMLEN(dp), dirp->enc); 
 -      } 
 -      else { 
 - 	 if (errno != 0) rb_sys_fail(0); 
 - 	 return Qnil; 		 /* end of stream */ 
 +      while ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) { 
 + 	 if (!DIR_IS_DOT_OR_DOTDOT(dp)) 
 + 	     return rb_external_str_new_with_enc(dp->d_name, NAMLEN(dp), dirp->enc); 
      } 
 +      if (errno != 0) rb_sys_fail(0); 
 +      return Qnil; 		 /* end of stream */ 
  } 
 
  /* 
 @@ -764,6 +765,7 @@ dir_each(VALUE dir) 
 	 const char *name = dp->d_name; 
 	 size_t namlen = NAMLEN(dp); 
 	 VALUE path; 
 + 	 if (DIR_IS_DOT_OR_DOTDOT(dp)) continue; 
  #if NORMALIZE_UTF8PATH 
 	 if (norm_p && has_nonascii(name, namlen) && 
 	     !NIL_P(path = rb_str_normalize_ospath(name, namlen))) { 
 diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb 
 index 2690a3f..33f0d44 100644 
 --- a/test/pathname/test_pathname.rb 
 +++ b/test/pathname/test_pathname.rb 
 @@ -1238,7 +1238,7 @@ def test_entries 
      with_tmpchdir('rubytest-pathname') {|dir| 
        open("a", "w") {} 
        open("b", "w") {} 
 -        assert_equal([Pathname("."), Pathname(".."), Pathname("a"), Pathname("b")], Pathname(".").entries.sort) 
 +        assert_equal([Pathname("a"), Pathname("b")], Pathname(".").entries.sort) 
      } 
    end 
 
 @@ -1248,7 +1248,7 @@ def test_each_entry 
        open("b", "w") {} 
        a = [] 
        Pathname(".").each_entry {|v| a << v } 
 -        assert_equal([Pathname("."), Pathname(".."), Pathname("a"), Pathname("b")], a.sort) 
 +        assert_equal([Pathname("a"), Pathname("b")], a.sort) 
      } 
    end 
 
 @@ -1278,7 +1278,7 @@ def test_opendir 
        Pathname(".").opendir {|d| 
          d.each {|e| a << e } 
        } 
 -        assert_equal([".", "..", "a", "b"], a.sort) 
 +        assert_equal(["a", "b"], a.sort) 
      } 
    end 
 
 diff --git a/test/ruby/test_dir.rb b/test/ruby/test_dir.rb 
 index 0cc5a6a..d3f6602 100644 
 --- a/test/ruby/test_dir.rb 
 +++ b/test/ruby/test_dir.rb 
 @@ -186,7 +186,7 @@ def test_glob_recursive 
 
    def assert_entries(entries) 
      entries.sort! 
 -      assert_equal(%w(. ..) + ("a".."z").to_a, entries) 
 +      assert_equal(("a".."z").to_a, entries) 
    end 
 
    def test_entries 
 ```

Back