123456789_123456789_123456789_123456789_123456789_

Class: IO

Relationships & Source Files
Inherits: Object
Defined in: ext/io/nonblock/nonblock.c

Instance Attribute Summary

Instance Attribute Details

#nonblock {|io| ... } ⇒ IO (rw) #nonblock(boolean) {|io| ... } ⇒ IO

Yields self in non-blocking mode.

When false is given as an argument, self is yielded in blocking mode. The original mode is restored after the block is executed.

[ GitHub ]

  
# File 'ext/io/nonblock/nonblock.c', line 109

static VALUE
rb_io_nonblock_block(int argc, VALUE *argv, VALUE io)
{
    int nb = 1;
    rb_io_t *fptr;
    int f, restore[2];

    GetOpenFile(io, fptr);
    if (argc > 0) {
	VALUE v;
	rb_scan_args(argc, argv, "01", &v);
	nb = RTEST(v);
    }
    f = io_nonblock_mode(fptr->fd);
    restore[0] = fptr->fd;
    restore[1] = f;
    if (!io_nonblock_set(fptr->fd, f, nb))
	return rb_yield(io);
    return rb_ensure(rb_yield, io, io_nonblock_restore, (VALUE)restore);
}

#nonblock=(boolean) ⇒ Boolean (rw)

Enables non-blocking mode on a stream when set to true, and blocking mode when set to false.

[ GitHub ]

  
# File 'ext/io/nonblock/nonblock.c', line 78

static VALUE
rb_io_nonblock_set(VALUE io, VALUE nb)
{
    rb_io_t *fptr;
    GetOpenFile(io, fptr);
    if (RTEST(nb))
	rb_io_set_nonblock(fptr);
    else
	io_nonblock_set(fptr->fd, io_nonblock_mode(fptr->fd), RTEST(nb));
    return io;
}

#nonblock?Boolean (rw)

Returns true if an IO object is in non-blocking mode.

[ GitHub ]

  
# File 'ext/io/nonblock/nonblock.c', line 39

static VALUE
rb_io_nonblock_p(VALUE io)
{
    rb_io_t *fptr;
    GetOpenFile(io, fptr);
    if (io_nonblock_mode(fptr->fd) & O_NONBLOCK)
	return Qtrue;
    return Qfalse;
}