Now, foo is part of a Ruby extension, but bar is user implemented.
The C implementation of foo can use rb_block_given_p() to identify if
it was called with a block. My question is: can I grab the bar method, save
its pointer somewhere and call it from other places in my code? I.e. is
there an option in the Ruby C API to parse/identify args in a given block?
I don’t want to yield bar, just save it and call it later (foo is actually a C++
constructor, which takes as an argument a pointer to a user function).
I don't want to yield bar, just save it and call it later (foo is
actually a C++ constructor, which takes as an argument a pointer to a
user function).
if (rb_block_given_p()) {
proc = rb_block_proc();
}
when you want to call it
rb_funcall(proc, rb_intern("call"), ...);
don't forget to define a mark function, and call rb_gc_mark(proc)
p.s. : for 1.6 this is rb_f_lambda() but this function is deprecated in 1.8