From 9c171e3d6b15209be8ff142ae97a632cc91a1bf8 Mon Sep 17 00:00:00 2001 From: yugui Date: Thu, 25 Dec 2008 09:54:17 +0000 Subject: merges r20946 from trunk into ruby_1_9_1. * io.c: rdoc for File::open and 1.9 feature in file modes. * transcode.c: rdoc for String#encode git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_1@21017 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- io.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 95 insertions(+), 28 deletions(-) (limited to 'io.c') diff --git a/io.c b/io.c index 520c062a0c..27da626ffe 100644 --- a/io.c +++ b/io.c @@ -4887,7 +4887,26 @@ rb_open_file(int argc, VALUE *argv, VALUE io) return io; } + +/* + * Document-method: File::open + * + * call-seq: + * File.open(filename, mode="r" [, opt]) => file + * File.open(filename [, mode [, perm]] [, opt]) => file + * File.open(filename, mode="r" [, opt]) {|file| block } => obj + * File.open(filename [, mode [, perm]] [, opt]) {|file| block } => obj + * + * With no associated block, open is a synonym for + * File.new. If the optional code block is given, it will + * be passed file as an argument, and the File object will + * automatically be closed when the block terminates. In this instance, + * File.open returns the value of the block. + */ + /* + * Document-method: IO::open + * * call-seq: * IO.open(fd, mode_string="r" [, opt] ) => io * IO.open(fd, mode_string="r" [, opt] ) {|io| block } => obj @@ -5788,6 +5807,48 @@ rb_io_stdio_file(rb_io_t *fptr) * IO object or integer file descriptor and mode * string. See also IO#fileno and * IO.for_fd. + * + * === Parameters + * fd:: numeric file descriptor + * mode:: file mode. a string or an integer + * opt:: hash for specifiying mode by name. + * + * ==== Mode + * When mode is an integer it must be combination of + * the modes defined in File::Constants. + * + * When mode is a string it must be in one of the + * following forms: + * - "fmode", + * - "fmode:extern", + * - "fmode:extern:intern". + * extern is the external encoding name for the IO. + * intern is the internal encoding. + * fmode must be combination of the directives. See + * the description of class +IO+ for a description of the directives. + * + * ==== Options + * opt can have the following keys + * :mode :: + * same as mode parameter + * :external_encoding :: + * external encoding for the IO. "-" is a + * synonym for the default external encoding. + * :internal_encoding :: + * internal encoding for the IO. + * "-" is a synonym for the default internal encoding. + * If the value is nil no conversion occurs. + * :encoding :: + * specifies external and internal encodings as "extern:intern". + * :textmode :: + * If the value is truth value, same as "b" in argument mode. + * :binmode :: + * If the value is truth value, same as "t" in argument mode. + * + * Also opt can have same keys in String#encode for + * controlling conversion between the external encoding and the internal encoding. + * + * === Example1 * * puts IO.new($stdout).fileno # => 1 * @@ -5799,6 +5860,16 @@ rb_io_stdio_file(rb_io_t *fptr) * * Hello * World + * + * === Example2 + * io = IO.new(2, mode: 'w:UTF-16LE', cr_newline: true) + * io.puts "Hello, World!" + * + * io = IO.new(2, mode: 'w', cr_newline: true, external_encoding: Encoding::UTF_16LE) + * io.puts "Hello, World!" + * + * both of aboves print "Hello, World!" in UTF-16LE to standard error output with + * converting EOL generated by puts to CR. */ static VALUE @@ -5835,22 +5906,28 @@ rb_io_initialize(int argc, VALUE *argv, VALUE io) return io; } - /* * call-seq: * File.new(filename, mode="r" [, opt]) => file * File.new(filename [, mode [, perm]] [, opt]) => file * - * Opens the file named by _filename_ according to * _mode_ (default is ``r'') and returns a new - * File object. See the description of class +IO+ for - * a description of _mode_. The file mode may optionally be - * specified as a +Fixnum+ by _or_-ing together the - * flags (O_RDONLY etc, again described under +IO+). Optional - * permission bits may be given in _perm_. These mode and permission - * bits are platform dependent; on Unix systems, see - * open(2) for details. + * File object. + * + * === Parameters + * See the description of class +IO+ for a description of _mode_. + * The file mode may optionally be specified as a +Fixnum+ + * by _or_-ing together the flags (O_RDONLY etc, + * again described under +IO+). + * + * Optional permission bits may be given in _perm_. + * These mode and permission bits are platform dependent; + * on Unix systems, see open(2) for details. + * + * Optional _opt_ parameter is same as in . + * + * === Examples * * f = File.new("testfile", "r") * f = File.new("newfile", "w+") @@ -5876,24 +5953,6 @@ rb_file_initialize(int argc, VALUE *argv, VALUE io) return io; } -/* - * call-seq: - * IO.new(fd, mode_string [, opt]) => io - * - * Returns a new IO object (a stream) for the given - * integer file descriptor and mode string. See also - * IO#fileno and IO.for_fd. - * - * a = IO.new(2,"w") # '2' is standard error - * $stderr.puts "Hello" - * a.puts "World" - * - * produces: - * - * Hello - * World - */ - static VALUE rb_io_s_new(int argc, VALUE *argv, VALUE klass) { @@ -8341,8 +8400,11 @@ rb_get_argv(void) * | otherwise creates a new file for reading and * | writing. * -----+-------------------------------------------------------- - * "b" | (DOS/Windows only) Binary file mode (may appear with + * "b" | Binary file mode (may appear with * | any of the key letters listed above). + * | Suppresses EOL <-> CRLF conversion on Windows. And + * | sets external encoding to ASCII-8BIT unless explicitly + * | specified. * -----+-------------------------------------------------------- * "t" | Text file mode (may appear with * | any of the key letters listed above except "b"). @@ -8405,6 +8467,11 @@ Init_IO(void) rb_cIO = rb_define_class("IO", rb_cObject); rb_include_module(rb_cIO, rb_mEnumerable); +#if 0 + /* This is necessary only for forcing rdoc handle File::open */ + rb_define_singleton_method(rb_cFile, "open", rb_io_s_open, -1); +#endif + rb_define_alloc_func(rb_cIO, io_alloc); rb_define_singleton_method(rb_cIO, "new", rb_io_s_new, -1); rb_define_singleton_method(rb_cIO, "open", rb_io_s_open, -1); -- cgit v1.2.3