summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornari <nari@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-03 01:37:50 +0000
committernari <nari@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-03 01:37:50 +0000
commit805b08f2925f5ceec67bf472e76e869bbddc8c39 (patch)
tree95071dbe2bf00ba5d91d7c0e3c0c137ec9d32265
parentd2fd7f32c8f4b134dc6716903be3be0f6c67760b (diff)
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37432 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog11
-rw-r--r--NEWS1
-rw-r--r--eval.c20
-rw-r--r--test/ruby/test_method.rb5
4 files changed, 37 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index fb17719065..d46ecf6bc4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+Sat Nov 3 10:17:41 2012 Narihiro Nakamura <authornari@gmail.com>
+
+ * 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@ruby-lang.org>
* test/ruby/test_refinement.rb (test_new_method_by_send,
diff --git a/NEWS b/NEWS
index 7afd422ced..2f7f5d98ad 100644
--- a/NEWS
+++ b/NEWS
@@ -41,6 +41,7 @@ with all sufficient information, see the ChangeLog file.
* 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
diff --git a/eval.c b/eval.c
index 21c33137f0..08a5624ba4 100644
--- a/eval.c
+++ b/eval.c
@@ -1556,6 +1556,25 @@ rb_f_callee_name(void)
}
}
+/*
+ * 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 @@ Init_eval(void)
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);
diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb
index 6a5a75cf20..a1572fde5e 100644
--- a/test/ruby/test_method.rb
+++ b/test/ruby/test_method.rb
@@ -489,4 +489,9 @@ class TestMethod < Test::Unit::TestCase
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