Skip to content

Commit 9373c5e

Browse files
committed
Pathname#empty? implemented.
* ext/pathname/pathname.c (Pathname#empty?): New method. [ruby-core:76404] [Feature#12596] Proposed by John Backus. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56571 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 19e5970 commit 9373c5e

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

Diff for: ChangeLog

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Sat Nov 5 12:14:31 2016 Tanaka Akira <akr@fsij.org>
2+
3+
* ext/pathname/pathname.c (Pathname#empty?): New method.
4+
[ruby-core:76404] [Feature#12596] Proposed by John Backus.
5+
16
Sat Nov 5 11:53:02 2016 Shugo Maeda <shugo@ruby-lang.org>
27

38
* test/ruby/test_refinement.rb (test_refine_alias_in_subclass):

Diff for: NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ with all sufficient information, see the ChangeLog file or Redmine
180180

181181
* Add an into option. [Feature #11191]
182182

183+
* pathname
184+
* New method: Pathname#empty? [Feature#12596]
185+
183186
* WEBrick
184187

185188
* Don't allow , as a separator [Bug #12791]

Diff for: ext/pathname/pathname.c

+17
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,22 @@ path_zero_p(VALUE self)
990990
return rb_funcall(rb_mFileTest, rb_intern("zero?"), 1, get_strpath(self));
991991
}
992992

993+
/*
994+
* Tests the file is empty.
995+
*
996+
* See Dir#empty? and FileTest.empty?.
997+
*/
998+
static VALUE
999+
path_empty_p(VALUE self)
1000+
{
1001+
1002+
VALUE path = get_strpath(self);
1003+
if (RTEST(rb_funcall(rb_mFileTest, rb_intern("directory?"), 1, path)))
1004+
return rb_funcall(rb_cDir, rb_intern("empty?"), 1, path);
1005+
else
1006+
return rb_funcall(rb_mFileTest, rb_intern("empty?"), 1, path);
1007+
}
1008+
9931009
static VALUE
9941010
glob_i(RB_BLOCK_CALL_FUNC_ARGLIST(elt, klass))
9951011
{
@@ -1452,6 +1468,7 @@ Init_pathname(void)
14521468
rb_define_method(rb_cPathname, "world_writable?", path_world_writable_p, 0);
14531469
rb_define_method(rb_cPathname, "writable_real?", path_writable_real_p, 0);
14541470
rb_define_method(rb_cPathname, "zero?", path_zero_p, 0);
1471+
rb_define_method(rb_cPathname, "empty?", path_empty_p, 0);
14551472
rb_define_singleton_method(rb_cPathname, "glob", path_s_glob, -1);
14561473
rb_define_singleton_method(rb_cPathname, "getwd", path_s_getwd, 0);
14571474
rb_define_singleton_method(rb_cPathname, "pwd", path_s_getwd, 0);

Diff for: test/pathname/test_pathname.rb

+14
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,20 @@ def test_zero?
12121212
}
12131213
end
12141214

1215+
def test_empty?
1216+
with_tmpchdir('rubytest-pathname') {|dir|
1217+
open("nonemptyfile", "w") {|f| f.write "abc" }
1218+
open("emptyfile", "w") {|f| }
1219+
Dir.mkdir("nonemptydir")
1220+
open("nonemptydir/somefile", "w") {|f| }
1221+
Dir.mkdir("emptydir")
1222+
assert_equal(true, Pathname("emptyfile").empty?)
1223+
assert_equal(false, Pathname("nonemptyfile").empty?)
1224+
assert_equal(true, Pathname("emptydir").empty?)
1225+
assert_equal(false, Pathname("nonemptydir").empty?)
1226+
}
1227+
end
1228+
12151229
def test_s_glob
12161230
with_tmpchdir('rubytest-pathname') {|dir|
12171231
open("f", "w") {|f| f.write "abc" }

0 commit comments

Comments
 (0)