Archive for August 12th, 2008

August 12, 2008

ruby blocks in the c api

m_func1 and m_func2 are both methods of a ruby class or module; m_func1 is implemented as:

m_func1(VALUE self) {

the problem is if a block is passed to m_func1 it seems to also be passed onto m_func2, as they’re both running
in the same ‘ruby frame’ (scope, local variables, self etc). How to prevent this happening?

(1) create a new ruby frame using rb_funcall:

m_func1(VALUE self) {
rb_funcall(self, rb_intern("func2"), 0);

(2) Because (1) can be quite slow it might be better to breaki out the core functionality of m_func2 into an internal
function (one that is not exposed to the ruby interpreter). m_func1 would call this internal function, and you would create a new m_func2 that handles a block and calls this same internal function.

Thanks to Tim Pease from ruby-forums for helping me understand this.

Tags: , ,