* 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
This commit is contained in:
nari 2012-11-03 01:37:50 +00:00
Родитель d2fd7f32c8
Коммит 805b08f292
4 изменённых файлов: 37 добавлений и 0 удалений

Просмотреть файл

@ -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,

1
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

20
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);

Просмотреть файл

@ -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