1

Perhaps more generally, how do you pass a block, written in C, to another C function that accepts a block? I know I can do something like this:

VALUE refine_foobar(VALUE block_arg, VALUE data, int argc, VALUE* argv) {
  // block code here
  return Qnil;
}

void Init_mything() {
  VALUE mod = rb_define_module("Foobar");
  rb_block_call(mod, rb_intern("refine"), 0, NULL, refine_foobar, Qnil);
}

but I feel like there has to be a way to call rb_mod_refine directly instead of going through rb_block_call.

Any ideas? Thanks!

Cameron
  • 641
  • 1
  • 7
  • 18
  • In a twist of fate, I have realized that `rb_mod_refine` isn't mentioned in the ruby.h header file, so maybe I'm not even supposed to use it :( – Cameron Apr 05 '19 at 16:08

1 Answers1

1

In current usage, refinements are applied to modules. It's right there in the method's name.

AFAIK, it does not directly work with blocks.

The only way I can see this working is to create a "C" method in a module and then use ruby code to apply that module as a refinement in the conventional manner.

Peter Camilleri
  • 1,882
  • 15
  • 17
  • Hmm I'm not sure I follow. Refinements are defined inside modules, but refinements are not themselves modules (at least not from a top-level API point of view). In other words I don't know if it's possible to "apply that module as a refinement." The module, defined in C, could be included inside the block passed to `refine` however. I'll give that a try. – Cameron Apr 05 '19 at 16:20
  • Refinements change a module with a scope. The include method imports methods from a module. If this is done in a refinement, then a module is being used to apply a changes to another module in that refinement. – Peter Camilleri Apr 05 '19 at 16:28
  • Yep, that's what I ended up trying, and it worked :) Thank you! – Cameron Apr 05 '19 at 17:19