代码之家  ›  专栏  ›  技术社区  ›  horseyguy

如何在Ruby C扩展中将Proc转换为块?

  •  1
  • horseyguy  · 技术社区  · 16 年前

    proc_list.each { |my_proc|
        @receiver.instance_eval(&my_proc)
    }
    

    1 回复  |  直到 13 年前
        1
  •  1
  •   rampion    16 年前

    摘自镐,第871页(1.9版)

    VALUE rb_iterate( VALUE (*method)(), VALUE args, VALUE (*block)(), VALUE arg2 )

    yield 从那 方法将调用带有yield和第二个参数的块 参数arg2。

    所以通过你的 Proc 对象如 arg2 并定义a (*block)() 函数,它只是将传递的值转发给 #call

    有点像

    for (i = 0; i < numProcs; i++)
    {
      rb_iterate( forwarder, receiver, block, procs[i] );
    }
    
    /*...*/
    
    VALUE forwarder(VALUE receiver)
    {
      // the block passed to #instance_eval will be the same block passed to forwarder
      return rb_obj_instance_eval(0, NULL, receiver);
    }
    VALUE block(VALUE proc)
    {
      return rb_funcall(proc, rb_intern("call"), 0);
    }
    

    我还没有测试这段代码,但它与中的警告一致 this article .