# Calling and passing blocks in a C extension

**URL:** <https://www.ruby-forum.com/t/calling-and-passing-blocks-in-a-c-extension/121023>\
**Category:** Ruby\
**Created:** [November 22, 2007, 9:34pm UTC](https://www.ruby-forum.com/t/calling-and-passing-blocks-in-a-c-extension/121023 "2007-11-22T21:34:15Z")\
**Posts on this page:** 3\
**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:** [November 22, 2007, 9:34pm UTC](https://www.ruby-forum.com/t/calling-and-passing-blocks-in-a-c-extension/121023/1 "2007-11-22T21:34:15Z")

</div>

Hello

How do I do the equivalent of the code below in a C extension?

def foo(&block)  
x = 0  
my\_iterator do |b|  
b.process(&block)  
x += 1  
end  
return x  
end

I tried lots of different combinations of rb\_iterate() and rb\_proc\_new()  
but it turned into one big mess :s

Thanks,  
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:** [November 23, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/calling-and-passing-blocks-in-a-c-extension/121023/2 "2007-11-23T14:42:53Z")

</div>

On Fri, 2007-11-23 at 05:33 +0900, Andre N. wrote:

> I tried lots of different combinations of rb\_iterate() and rb\_proc\_new()  
> but it turned into one big mess :s

I’ve just noticed it works in ruby 1.9 but segfaults in 1.8.6… Can  
anyone spot a reason? (It seems to be segfaulting when rb\_yield() is  
called in my\_iterator()).

#include \<ruby.h\>

static VALUE  
call\_block(VALUE x, VALUE block)  
{  
return rb\_funcall(block, rb\_intern(“call”), 1, x);  
}

static VALUE  
process\_i(VALUE b, void \*data)  
{  
VALUE \*d = (VALUE \*)data;  
int \*x = (int \*)d[0];  
VALUE block = d[1];

```
*x += 1;
return rb_iterate(rb_ary_each, b, call_block, block);

```

}

static VALUE  
my\_iterator(VALUE dummy)  
{  
VALUE ary = rb\_ary\_new3(2, INT2FIX(10), INT2FIX(20));  
return rb\_yield(ary);  
}

static VALUE  
foo(int argc, VALUE \*argv, VALUE self)  
{  
VALUE block;  
int x = 0;  
VALUE data[2];  
data[0] = (VALUE)&x;  
data[1] = block;  
rb\_scan\_args(argc, argv, “00&”, &block);  
rb\_iterate(my\_iterator, 0, process\_i, (VALUE)data);  
return(self);  
}

void  
Init\_a(void)  
{  
VALUE cA = rb\_define\_class(“A”, rb\_cObject);  
rb\_define\_method(cA, “initialize”, foo, -1);  
}

Thanks,  
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:** [November 23, 2007, 3:38pm UTC](https://www.ruby-forum.com/t/calling-and-passing-blocks-in-a-c-extension/121023/3 "2007-11-23T15:38:31Z")

</div>

Guy Decoux pointed me my stupid mistake…

> static VALUE  
> foo(int argc, VALUE \*argv, VALUE self)  
> {  
> VALUE block;  
> int x = 0;  
> VALUE data[2];

- 

```
rb_scan_args(argc, argv, "00&", &block);

```

> ```
> data[0] = (VALUE)&x;
> data[1] = block;
> 
> ```

- 

```
rb_scan_args(argc, argv, "00&", &block);

```

> ```
> rb_iterate(my_iterator, 0, process_i, (VALUE)data);
> return(self);
> 
> ```
> 
> }

Andre
