Project

General

Profile

Feature #3346 ยป __dir__.patch

authorNari (Narihiro Nakamura), 11/01/2012 11:02 PM

View differences:

eval.c
1537 1537
    }
1538 1538
}
1539 1539

  
1540
/*
1541
 *  call-seq:
1542
 *     __dir__         -> string
1543
 *
1544
 *  Returns the value of <code>File.dirname(__FILE__)</code>
1545
 *  If <code>__FILE__</code> is <code>nil</code>, it returns <code>nil</code>.
1546
 *
1547
 */
1548
static VALUE
1549
f_current_dirname(void)
1550
{
1551
    VALUE base = rb_current_realfilepath();
1552
    if (NIL_P(base)) {
1553
	return Qnil;
1554
    }
1555
    base = rb_file_dirname(base);
1556
    return base;
1557
}
1558

  
1540 1559
void
1541 1560
Init_eval(void)
1542 1561
{
......
1550 1569

  
1551 1570
    rb_define_global_function("__method__", rb_f_method_name, 0);
1552 1571
    rb_define_global_function("__callee__", rb_f_callee_name, 0);
1572
    rb_define_global_function("__dir__", f_current_dirname, 0);
1553 1573

  
1554 1574
    rb_define_private_method(rb_cModule, "append_features", rb_mod_append_features, 1);
1555 1575
    rb_define_private_method(rb_cModule, "extend_object", rb_mod_extend_object, 1);
test/ruby/test_method.rb
489 489
      1000.times {p = Bug6171.new('test'); 10000.times {p.reverse}}
490 490
      EOC
491 491
  end
492

  
493
  def test___dir__
494
    assert_instance_of String, __dir__
495
    assert_equal(File.dirname(__FILE__), __dir__)
496
  end
492 497
end