diff --git a/file.c b/file.c index f2b760a..d98d958 100644 --- a/file.c +++ b/file.c @@ -2867,6 +2867,19 @@ rb_file_s_unlink(VALUE klass, VALUE args) return LONG2FIX(n); } +static VALUE +rb_file_unlink(VALUE obj) +{ + rb_io_t *fptr; + const char *s; + + GetOpenFile(obj, fptr); + s = RSTRING_PTR(fptr->pathv); + unlink_internal(s, fptr->pathv, 0); + + return Qnil; +} + /* * call-seq: * File.rename(old_name, new_name) -> 0 @@ -5999,6 +6012,7 @@ Init_File(void) rb_define_method(rb_cFile, "truncate", rb_file_truncate, 1); rb_define_method(rb_cFile, "flock", rb_file_flock, 1); + rb_define_method(rb_cFile, "unlink", rb_file_unlink, 0); /* * Document-module: File::Constants diff --git a/test/ruby/test_file_exhaustive.rb b/test/ruby/test_file_exhaustive.rb index e4b9215..e9129e0 100644 --- a/test/ruby/test_file_exhaustive.rb +++ b/test/ruby/test_file_exhaustive.rb @@ -560,6 +560,14 @@ class TestFileExhaustive < Test::Unit::TestCase assert_equal(1, File.unlink(regular_file)) make_file("foo", regular_file) assert_raise(Errno::ENOENT) { File.unlink(nofile) } + + assert_equal(nil, File.open(regular_file){|f| f.unlink}) + assert_file.not_exist?(regular_file) + make_file("foo", regular_file) + + assert_raise(Errno::ENOENT) { File.open(regular_file){|f| f.unlink; f.unlink} } + assert_file.not_exist?(regular_file) + make_file("foo", regular_file) end def test_rename