# Passing blocks around in a C extension

**URL:** <https://www.ruby-forum.com/t/passing-blocks-around-in-a-c-extension/66602>\
**Category:** Ruby\
**Created:** [August 27, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/passing-blocks-around-in-a-c-extension/66602 "2006-08-27T18:56:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![reflex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@reflex](https://www.ruby-forum.com/u/reflex)\
**Post date:** [August 27, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/passing-blocks-around-in-a-c-extension/66602/1 "2006-08-27T18:56:54Z")

</div>

Hi

I’m trying to wrap up a library, and one of its functions accepts a  
callback function as an argument. What I want to do is to make the  
function exported to ruby accept a block, and yield this block on the  
callback function. So, to do that, I guess what I need is some way to do  
the equivalent of

def foo(&block)  
…  
end

in C, ie, store the code block in a variable so I can pass it around.

I could make my code work using a Proc object explicitely as one of the  
C function’s arguments, as in

static VALUE  
my\_func(…, VALUE block)  
{  
…  
func\_with\_callback(…, my\_callback, (void \*)block);  
…  
}

static void  
my\_callback(void \*arg)  
{  
rb\_funcall((VALUE)arg, rb\_intern(“call”), 0);  
}

And from ruby I use it as

obj.my\_func(proc { puts ‘blah’ })

How could I be able to call it as

obj.my\_func { puts ‘blah’ }

and still be able to pass the code block to the callback function?

Thanks in advance,  
Andre

---

<div class="post-metadata">

**Author:** ![reflex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@reflex](https://www.ruby-forum.com/u/reflex)\
**Post date:** [August 27, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/passing-blocks-around-in-a-c-extension/66602/2 "2006-08-27T19:08:55Z")

</div>

> > > > > “A” == Andre N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

A\> def foo(&block)

In ruby, this can be seen like this

```
 def foo
    block = Proc.new if block_given?

```

A\> …  
A\> end

A\> in C, ie, store the code block in a variable so I can pass it around.

Proc.new is rb\_block\_proc()  
block\_given? is rb\_block\_given\_p()

Guy Decoux

---

<div class="post-metadata">

**Author:** ![reflex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@reflex](https://www.ruby-forum.com/u/reflex)\
**Post date:** [August 27, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/passing-blocks-around-in-a-c-extension/66602/3 "2006-08-27T19:48:57Z")

</div>

On Mon, 2006-08-28 at 02:06 +0900, ts wrote:

> Proc.new is rb\_block\_proc()  
> block\_given? is rb\_block\_given\_p()

Thanks Guy!

Now, it must be something stupid, but this works:

func\_with\_callback(…, my\_callback, (void \*)rb\_block\_proc());

and this segfaults:

VALUE block = rb\_block\_proc();  
func\_with\_callback(…, my\_callback, (void \*)block);

Any idea why? I don’t really need the second form, but it kinda puzzled  
me.

Thanks again,  
Andre

---

<div class="post-metadata">

**Author:** ![reflex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@reflex](https://www.ruby-forum.com/u/reflex)\
**Post date:** [August 28, 2006, 10:24am UTC](https://www.ruby-forum.com/t/passing-blocks-around-in-a-c-extension/66602/4 "2006-08-28T10:24:16Z")

</div>

> > > > > “A” == Andre N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

A\> Any idea why?

Well, I must see the source but if the proc is called after the end of  
the  
C function it must be protected against the GC.

Guy Decoux
