static public Method

File.new(...)

File.new(filename, mode="r")             file
File.new(filename [, mode [, perm]])     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.

f = File.new("testfile", "r")
f = File.new("newfile",  "w+")
f = File.new("newfile", File::CREAT|File::TRUNC|File::RDWR, 0644)

Source Code

/*
*  call-seq:
*     File.new(filename, mode="r")            => file
*     File.new(filename [, mode [, perm]])    => file
*  

*  Opens the file named by _filename_ according to
*  _mode_ (default is ``r'') and returns a new
*  <code>File</code> 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
*  <code>open(2)</code> for details.
*
*     f = File.new("testfile", "r")
*     f = File.new("newfile",  "w+")
*     f = File.new("newfile", File::CREAT|File::TRUNC|File::RDWR, 0644)
*/

static VALUE
rb_file_initialize(argc, argv, io)
   int argc;
   VALUE *argv;
   VALUE io;
{
   if (RFILE(io)->fptr) {
       rb_raise(rb_eRuntimeError, "reinitializing File");
   }
   if (0 < argc && argc < 3) {
       VALUE fd = rb_check_convert_type(argv[0], T_FIXNUM, "Fixnum", "to_int");

       if (!NIL_P(fd)) {
           argv[0] = fd;
           return rb_io_initialize(argc, argv, io);
       }
   }
   rb_open_file(argc, argv, io);

   return io;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.