diff options
-rw-r--r-- | ChangeLog | 11 | ||||
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | eval.c | 20 | ||||
-rw-r--r-- | test/ruby/test_method.rb | 5 |
4 files changed, 37 insertions, 0 deletions
@@ -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, @@ -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 @@ -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 |