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

ruby-changes:25375

From: nari <ko1@a...>
Date: Sat, 3 Nov 2012 10:38:11 +0900 (JST)
Subject: [ruby-changes:25375] nari:r37432 (trunk): * eval.c (f_current_dirname): add the new method for Kernel.

nari	2012-11-03 10:37:50 +0900 (Sat, 03 Nov 2012)

  New Revision: 37432

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37432

  Log:
    * eval.c (f_current_dirname): add the new method for Kernel.
      This method almotst same as File.dirname(__FILE__). One
      different behavior is it returns nil when __FILE__ returns nil.
      [Feature #3346]
    
    * NEWS:  ditto
    
    * test/ruby/test_method.rb: related test.

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/eval.c
    trunk/test/ruby/test_method.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 37431)
+++ ChangeLog	(revision 37432)
@@ -1,3 +1,14 @@
+Sat Nov  3 10:17:41 2012  Narihiro Nakamura  <authornari@g...>
+
+	* eval.c (f_current_dirname): add the new method for Kernel.
+	  This method almotst same as File.dirname(__FILE__). One
+	  different behavior is it returns nil when __FILE__ returns nil.
+	  [Feature #3346]
+
+	* NEWS:  ditto
+
+	* test/ruby/test_method.rb: related test.
+
 Sat Nov  3 09:03:34 2012  Shugo Maeda  <shugo@r...>
 
 	* test/ruby/test_refinement.rb (test_new_method_by_send,
Index: eval.c
===================================================================
--- eval.c	(revision 37431)
+++ eval.c	(revision 37432)
@@ -1556,6 +1556,25 @@
     }
 }
 
+/*
+ *  call-seq:
+ *     __dir__         -> string
+ *
+ *  Returns the value of <code>File.dirname(__FILE__)</code>
+ *  If <code>__FILE__</code> is <code>nil</code>, it returns <code>nil</code>.
+ *
+ */
+static VALUE
+f_current_dirname(void)
+{
+    VALUE base = rb_current_realfilepath();
+    if (NIL_P(base)) {
+	return Qnil;
+    }
+    base = rb_file_dirname(base);
+    return base;
+}
+
 void
 Init_eval(void)
 {
@@ -1569,6 +1588,7 @@
 
     rb_define_global_function("__method__", rb_f_method_name, 0);
     rb_define_global_function("__callee__", rb_f_callee_name, 0);
+    rb_define_global_function("__dir__", f_current_dirname, 0);
 
     rb_define_private_method(rb_cModule, "append_features", rb_mod_append_features, 1);
     rb_define_private_method(rb_cModule, "extend_object", rb_mod_extend_object, 1);
Index: NEWS
===================================================================
--- NEWS	(revision 37431)
+++ NEWS	(revision 37432)
@@ -41,6 +41,7 @@
       * added Kernel#Hash conversion method like Array() or Float().
       * added Kernel#using, which imports refinements into the current scope.
         [experimental]
+      * added Kernel#__dir__ which returns a current dirname.
     * extended method:
       * Kernel#warn accepts multiple args in like puts.
       * Kernel#caller accepts second optional argument `n' which specify
Index: test/ruby/test_method.rb
===================================================================
--- test/ruby/test_method.rb	(revision 37431)
+++ test/ruby/test_method.rb	(revision 37432)
@@ -489,4 +489,9 @@
       1000.times {p = Bug6171.new('test'); 10000.times {p.reverse}}
       EOC
   end
+
+  def test___dir__
+    assert_instance_of String, __dir__
+    assert_equal(File.dirname(__FILE__), __dir__)
+  end
 end

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

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