1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/pathname/pathname.c (path_open): Pathname#open

translated from pathname.rb.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28941 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2010-08-09 13:15:49 +00:00
parent 55584cb206
commit ec5bb26fa0
4 changed files with 44 additions and 6 deletions

View file

@ -1,3 +1,8 @@
Mon Aug 9 22:15:19 2010 Tanaka Akira <akr@fsij.org>
* ext/pathname/pathname.c (path_open): Pathname#open
translated from pathname.rb.
Mon Aug 9 22:11:09 2010 Tanaka Akira <akr@fsij.org>
* test/ruby/test_signal.rb (TestSignal#test_exit_action): use

View file

@ -512,11 +512,6 @@ end
class Pathname # * File *
# See <tt>File.open</tt>. Opens the file for reading or writing.
def open(*args, &block) # :yield: file
File.open(@path, *args, &block)
end
# See <tt>File.readlink</tt>. Read symbolic link.
def readlink() self.class.new(File.readlink(@path)) end

View file

@ -336,6 +336,25 @@ path_make_link(VALUE self, VALUE old)
return rb_funcall(rb_cFile, rb_intern("link"), 2, old, get_strpath(self));
}
/*
* See <tt>File.open</tt>. Opens the file for reading or writing.
*/
static VALUE
path_open(int argc, VALUE *argv, VALUE self)
{
VALUE args[4];
int n;
args[0] = get_strpath(self);
n = rb_scan_args(argc, argv, "03", &args[1], &args[2], &args[3]);
if (rb_block_given_p()) {
return rb_block_call(rb_cFile, rb_intern("open"), 1+n, args, 0, 0);
}
else {
return rb_funcall2(rb_cFile, rb_intern("open"), 1+n, args);
}
}
/*
* == Pathname
*
@ -547,4 +566,5 @@ Init_pathname()
rb_define_method(rb_cPathname, "fnmatch?", path_fnmatch, -1);
rb_define_method(rb_cPathname, "ftype", path_ftype, 0);
rb_define_method(rb_cPathname, "make_link", path_make_link, 1);
rb_define_method(rb_cPathname, "open", path_open, -1);
}

View file

@ -818,9 +818,27 @@ class TestPathname < Test::Unit::TestCase
def test_open
with_tmpchdir('rubytest-pathname') {|dir|
open("a", "w") {|f| f.write "abc" }
Pathname("a").open {|f|
path = Pathname("a")
path.open {|f|
assert_equal("abc", f.read)
}
path.open("r") {|f|
assert_equal("abc", f.read)
}
Pathname("b").open("w", 0444) {|f| f.write "def" }
assert_equal(0444, File.stat("b").mode & 0777)
assert_equal("def", File.read("b"))
Pathname("c").open("w", 0444, {}) {|f| f.write "ghi" }
assert_equal(0444, File.stat("c").mode & 0777)
assert_equal("ghi", File.read("c"))
g = path.open
assert_equal("abc", g.read)
g.close
}
end