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: , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: