From bb62f0c43d20b4b179422088b378766e76dc16f9 Mon Sep 17 00:00:00 2001 From: niku Date: Fri, 1 Jun 2012 18:07:15 +0900 Subject: [PATCH] ext/pathname/pathname.c add Pathname#write and Pathname#binwrite --- ext/pathname/pathname.c | 42 ++++++++++++++++++++++++++++++++++++++++ test/pathname/test_pathname.rb | 14 ++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/ext/pathname/pathname.c b/ext/pathname/pathname.c index f890019..85ff72b 100644 --- a/ext/pathname/pathname.c +++ b/ext/pathname/pathname.c @@ -340,6 +340,44 @@ path_sysopen(int argc, VALUE *argv, VALUE self) } /* + * call-seq: + * pathname.write(string, [offset]) -> fixnum + * pathname.write(string, [offset], open_args) -> fixnum + * + * See IO.write. Returns the byte length written. + * + */ +static VALUE +path_write(int argc, VALUE *argv, VALUE self) +{ + VALUE args[4]; + int n; + + args[0] = get_strpath(self); + n = rb_scan_args(argc, argv, "12", &args[1], &args[2], &args[3]); + return rb_funcall2(rb_cIO, rb_intern("write"), 1+n, args); +} + +/* + * call-seq: + * pathname.binwrite(string, [offset]) -> fixnum + * pathname.binwrite(string, [offset], open_args) -> fixnum + * + * See IO.binwrite. Returns the byte length written. + * + */ +static VALUE +path_binwrite(int argc, VALUE *argv, VALUE self) +{ + VALUE args[4]; + int n; + + args[0] = get_strpath(self); + n = rb_scan_args(argc, argv, "12", &args[1], &args[2], &args[3]); + return rb_funcall2(rb_cIO, rb_intern("binwrite"), 1+n, args); +} + +/* * See File.atime. Returns last access time. */ static VALUE @@ -1151,6 +1189,8 @@ path_f_pathname(VALUE self, VALUE str) * - #binread(*args) * - #readlines(*args) * - #sysopen(*args) + * - #write(string, *args) + * - #binwrite(string, *args) * * === Utilities * @@ -1197,6 +1237,8 @@ Init_pathname() rb_define_method(rb_cPathname, "binread", path_binread, -1); rb_define_method(rb_cPathname, "readlines", path_readlines, -1); rb_define_method(rb_cPathname, "sysopen", path_sysopen, -1); + rb_define_method(rb_cPathname, "write", path_write, -1); + rb_define_method(rb_cPathname, "binwrite", path_binwrite, -1); rb_define_method(rb_cPathname, "atime", path_atime, 0); rb_define_method(rb_cPathname, "ctime", path_ctime, 0); rb_define_method(rb_cPathname, "mtime", path_mtime, 0); diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb index ccdc5bd..d3a578d 100644 --- a/test/pathname/test_pathname.rb +++ b/test/pathname/test_pathname.rb @@ -729,6 +729,20 @@ class TestPathname < Test::Unit::TestCase } end + def test_write + with_tmpchdir('rubytest-pathname') {|dir| + Pathname("a").write("1\n2\n") + assert_equal("1\n2\n", open("a") {|f| f.read }) + } + end + + def test_binwrite + with_tmpchdir('rubytest-pathname') {|dir| + Pathname("a").binwrite("abc") + assert_equal("abc", open("a") {|f| f.read }) + } + end + def test_atime assert_kind_of(Time, Pathname(__FILE__).atime) end -- 1.7.10.1