Skip to content

Instantly share code, notes, and snippets.

@patrickt
Created March 10, 2009 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickt/76998 to your computer and use it in GitHub Desktop.
Save patrickt/76998 to your computer and use it in GitHub Desktop.
static VALUE
io_write(VALUE io, VALUE str)
{
rb_io_t *fptr;
long n;
VALUE tmp;
rb_secure(4);
io = GetWriteIO(io);
str = rb_obj_as_string(str);
tmp = rb_io_check_io(io);
if (NIL_P(tmp)) {
/* port is not IO, call write method for it. */
return rb_funcall(io, id_write, 1, str);
}
io = tmp;
if (RSTRING_LEN(str) == 0) return INT2FIX(0);
GetOpenFile(io, fptr);
rb_io_check_writable(fptr);
n = io_fwrite(str, fptr);
if (n == -1L) rb_sys_fail(fptr->path);
return LONG2FIX(n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment