[前][次][番号順一覧][スレッド一覧]

ruby-changes:44498

From: akr <ko1@a...>
Date: Sat, 5 Nov 2016 12:29:12 +0900 (JST)
Subject: [ruby-changes:44498] akr:r56571 (trunk): Pathname#empty? implemented.

akr	2016-11-05 12:29:08 +0900 (Sat, 05 Nov 2016)

  New Revision: 56571

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56571

  Log:
    Pathname#empty? implemented.
    
    * ext/pathname/pathname.c (Pathname#empty?): New method.
      [ruby-core:76404] [Feature#12596] Proposed by John Backus.

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/ext/pathname/pathname.c
    trunk/test/pathname/test_pathname.rb
Index: NEWS
===================================================================
--- NEWS	(revision 56570)
+++ NEWS	(revision 56571)
@@ -180,6 +180,9 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L180
 
   * Add an into option. [Feature #11191]
 
+* pathname
+  * New method: Pathname#empty? [Feature#12596]
+
 * WEBrick
 
   * Don't allow , as a separator [Bug #12791]
Index: test/pathname/test_pathname.rb
===================================================================
--- test/pathname/test_pathname.rb	(revision 56570)
+++ test/pathname/test_pathname.rb	(revision 56571)
@@ -1212,6 +1212,20 @@ class TestPathname < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/pathname/test_pathname.rb#L1212
     }
   end
 
+  def test_empty?
+    with_tmpchdir('rubytest-pathname') {|dir|
+      open("nonemptyfile", "w") {|f| f.write "abc" }
+      open("emptyfile", "w") {|f| }
+      Dir.mkdir("nonemptydir")
+      open("nonemptydir/somefile", "w") {|f| }
+      Dir.mkdir("emptydir")
+      assert_equal(true, Pathname("emptyfile").empty?)
+      assert_equal(false, Pathname("nonemptyfile").empty?)
+      assert_equal(true, Pathname("emptydir").empty?)
+      assert_equal(false, Pathname("nonemptydir").empty?)
+    }
+  end
+
   def test_s_glob
     with_tmpchdir('rubytest-pathname') {|dir|
       open("f", "w") {|f| f.write "abc" }
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 56570)
+++ ChangeLog	(revision 56571)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Nov  5 12:14:31 2016  Tanaka Akira  <akr@f...>
+
+	* ext/pathname/pathname.c (Pathname#empty?): New method.
+	  [ruby-core:76404] [Feature#12596] Proposed by John Backus.
+
 Sat Nov  5 11:53:02 2016  Shugo Maeda  <shugo@r...>
 
 	* test/ruby/test_refinement.rb (test_refine_alias_in_subclass):
Index: ext/pathname/pathname.c
===================================================================
--- ext/pathname/pathname.c	(revision 56570)
+++ ext/pathname/pathname.c	(revision 56571)
@@ -990,6 +990,22 @@ path_zero_p(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/pathname/pathname.c#L990
     return rb_funcall(rb_mFileTest, rb_intern("zero?"), 1, get_strpath(self));
 }
 
+/*
+ * Tests the file is empty.
+ *
+ * See Dir#empty? and FileTest.empty?.
+ */
+static VALUE
+path_empty_p(VALUE self)
+{
+
+    VALUE path = get_strpath(self);
+    if (RTEST(rb_funcall(rb_mFileTest, rb_intern("directory?"), 1, path)))
+        return rb_funcall(rb_cDir, rb_intern("empty?"), 1, path);
+    else
+        return rb_funcall(rb_mFileTest, rb_intern("empty?"), 1, path);
+}
+
 static VALUE
 glob_i(RB_BLOCK_CALL_FUNC_ARGLIST(elt, klass))
 {
@@ -1452,6 +1468,7 @@ Init_pathname(void) https://github.com/ruby/ruby/blob/trunk/ext/pathname/pathname.c#L1468
     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_method(rb_cPathname, "empty?", path_empty_p, 0);
     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);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]