Class: Enumerator::Yielder

Inherits:
Object show all
Defined in:
enumerator.c

Instance Method Summary collapse

Constructor Details

#initializeObject

:nodoc:


1320
1321
1322
1323
1324
1325
1326
# File 'enumerator.c', line 1320

static VALUE
yielder_initialize(VALUE obj)
{
    rb_need_block();

    return yielder_init(obj, rb_block_proc());
}

Instance Method Details

#<<(arg) ⇒ Object

:nodoc:


1338
1339
1340
1341
1342
1343
1344
1345
1346
# File 'enumerator.c', line 1338

static VALUE
yielder_yield_push(VALUE obj, VALUE arg)
{
    struct yielder *ptr = yielder_ptr(obj);

    rb_proc_call_with_block(ptr->proc, 1, &arg, Qnil);

    return obj;
}

#to_procObject

Returns a Proc object that takes arguments and yields them.

This method is implemented so that a Yielder object can be directly passed to another method as a block argument.

enum = Enumerator.new { |y|
  Dir.glob("*.rb") { |file|
    File.open(file) { |f| f.each_line(&y) }
  }
}

1360
1361
1362
1363
1364
1365
1366
# File 'enumerator.c', line 1360

static VALUE
yielder_to_proc(VALUE obj)
{
    VALUE method = rb_obj_method(obj, sym_yield);

    return rb_funcall(method, idTo_proc, 0);
}

#yield(args) ⇒ Object

:nodoc:


1329
1330
1331
1332
1333
1334
1335
# File 'enumerator.c', line 1329

static VALUE
yielder_yield(VALUE obj, VALUE args)
{
    struct yielder *ptr = yielder_ptr(obj);

    return rb_proc_call_kw(ptr->proc, args, RB_PASS_CALLED_KEYWORDS);
}