static public Method

IO.new(...)

IO.new(fd, mode)    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

Source Code

/*
*  call-seq:
*     IO.new(fd, mode)   => io
*  
*  Returns a new <code>IO</code> object (a stream) for the given
*  integer file descriptor and mode string. See also
*  <code>IO#fileno</code> and <code>IO::for_fd</code>.
*     
*     a = IO.new(2,"w")      # '2' is standard error
*     $stderr.puts "Hello"
*     a.puts "World"
*     
*  <em>produces:</em>
*     
*     Hello
*     World
*/

static VALUE
rb_io_initialize(argc, argv, io)
   int argc;
   VALUE *argv;
   VALUE io;
{
   VALUE fnum, mode;
   OpenFile *fp;
   int fd, flags;

   rb_secure(4);
   rb_scan_args(argc, argv, "11", &fnum, &mode);
   fd = NUM2INT(fnum);
   if (argc == 2) {
       if (FIXNUM_P(mode)) {
           flags = FIX2LONG(mode);
       }
       else {
           SafeStringValue(mode);
           flags = rb_io_mode_modenum(RSTRING(mode)->ptr);
       }
   }
   else {
#if defined(HAVE_FCNTL) && defined(F_GETFL)
       flags = fcntl(fd, F_GETFL);
       if (flags == -1) rb_sys_fail(0);
#else
       flags = O_RDONLY;
#endif
   }
   MakeOpenFile(io, fp);
   fp->mode = rb_io_modenum_flags(flags);
   fp->f = rb_fdopen(fd, rb_io_modenum_mode(flags));

   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.