# Passing block in C

**URL:** <https://www.ruby-forum.com/t/passing-block-in-c/164178>\
**Category:** Ruby\
**Created:** [March 29, 2009, 4:31pm UTC](https://www.ruby-forum.com/t/passing-block-in-c/164178 "2009-03-29T16:31:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@peterz](https://www.ruby-forum.com/u/peterz)\
**Post date:** [March 29, 2009, 4:31pm UTC](https://www.ruby-forum.com/t/passing-block-in-c/164178/1 "2009-03-29T16:31:29Z")

</div>

Hi.

How can I call Ruby function from C with block?  
E. g.

VALUE block = rb\_proc\_new(…);  
rb\_funcall\_with\_block(cMyClass, rb\_intern(“initialize”), 0, block);

WBR, Peter Z.

---

<div class="post-metadata">

**Author:** ![peterz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@peterz](https://www.ruby-forum.com/u/peterz)\
**Post date:** [March 29, 2009, 5:01pm UTC](https://www.ruby-forum.com/t/passing-block-in-c/164178/2 "2009-03-29T17:01:57Z")

</div>

Peter Z. wrote:

> How can I call Ruby function from C with block?  
> E. g.
> 
> VALUE block = rb\_proc\_new(…);  
> rb\_funcall\_with\_block(cMyClass, rb\_intern(“initialize”), 0, block);

Let me Google that for you!

[http://www.google.com/codesearch?q=rb\_proc\_new](http://www.google.com/codesearch?q=rb_proc_new)

---

<div class="post-metadata">

**Author:** ![peterz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@peterz](https://www.ruby-forum.com/u/peterz)\
**Post date:** [March 29, 2009, 5:58pm UTC](https://www.ruby-forum.com/t/passing-block-in-c/164178/3 "2009-03-29T17:58:51Z")

</div>

Quoting P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> [http://www.google.com/codesearch?q=rb\_proc\_new](http://www.google.com/codesearch?q=rb_proc_new)  
> Ehm. I meant ‘what function must be in place of  
> rb\_funcall\_with\_block’. I _know_ how do rb\_proc\_new work.

WBR, Peter Z.

---

<div class="post-metadata">

**Author:** ![peterz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@peterz](https://www.ruby-forum.com/u/peterz)\
**Post date:** [March 29, 2009, 6:24pm UTC](https://www.ruby-forum.com/t/passing-block-in-c/164178/4 "2009-03-29T18:24:40Z")

</div>

Quoting “Robert K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > Let me Google that for you!
> 
> Which proves that his suggested search was indeed helpful. 🙂

It isn’t. See RubyDoc:

ObjectSpace.define\_finalizer(obj, aProc=proc())  
Adds aProc as a finalizer, to be called after obj was destroyed.

Notice the aProc is an argument, not block.  
So that code is analogue for Ruby  
method(object, proc { some\_code })  
and I need  
method(object) { some\_code}

WBR, Peter Z.

---

<div class="post-metadata">

**Author:** ![peterz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@peterz](https://www.ruby-forum.com/u/peterz)\
**Post date:** [March 29, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/passing-block-in-c/164178/5 "2009-03-29T18:53:28Z")

</div>

Hi Peter,

On Sun, 2009-03-29 at 23:30 +0900, Peter Z. wrote:

> Hi.
> 
> How can I call Ruby function from C with block?

You can use rb\_iterate():

static VALUE  
foo\_i(VALUE x, VALUE dummy)  
{  
rb\_p(x);  
return Qnil;  
}

static VALUE  
rb\_foo(VALUE self)  
{  
rb\_yield(INT2FIX(42));  
return Qnil;  
}

static VALUE  
call\_foo(VALUE klass)  
{  
return rb\_funcall(klass, rb\_intern(“foo”), 0);  
}

void  
Init\_myclass(void)  
{  
VALUE klass = rb\_define\_class(“MyClass”, rb\_cObject);  
rb\_define\_singleton\_method(klass, “foo”, rb\_foo, 0);  
rb\_iterate(call\_foo, klass, foo\_i, (VALUE)NULL);  
}

Best,  
Andre

---

<div class="post-metadata">

**Author:** ![peterz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@peterz](https://www.ruby-forum.com/u/peterz)\
**Post date:** [March 29, 2009, 6:15pm UTC](https://www.ruby-forum.com/t/passing-block-in-c/164178/6 "2009-03-29T18:15:51Z")

</div>

On 29.03.2009 17:58, Peter Z. wrote:

> > [http://www.google.com/codesearch?q=rb\_proc\_new](http://www.google.com/codesearch?q=rb_proc_new)
> 
> Ehm. I meant ‘what function must be in place of  
> rb\_funcall\_with\_block’. I _know_ how do rb\_proc\_new work.

I think that Phlip understood this. If you look at the results you’ll  
find this

[http://www.google.com/codesearch/p?hl=de#X2brleciKjw/trunk/devel/RubyClass.cpp&q=rb\_proc\_new](http://www.google.com/codesearch/p?hl=de#X2brleciKjw/trunk/devel/RubyClass.cpp&q=rb_proc_new)

Which proves that his suggested search was indeed helpful. 🙂

Cheers

robert

---

<div class="post-metadata">

**Author:** ![peterz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@peterz](https://www.ruby-forum.com/u/peterz)\
**Post date:** [March 29, 2009, 7:47pm UTC](https://www.ruby-forum.com/t/passing-block-in-c/164178/7 "2009-03-29T19:47:26Z")

</div>

> I think that Phlip understood this.

Actually I didn’t. I was just having a “let me Google CodeSearch that  
for you”  
moment… (-:

---

<div class="post-metadata">

**Author:** ![peterz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@peterz](https://www.ruby-forum.com/u/peterz)\
**Post date:** [March 29, 2009, 7:55pm UTC](https://www.ruby-forum.com/t/passing-block-in-c/164178/8 "2009-03-29T19:55:39Z")

</div>

On 29.03.2009 18:23, Peter Z. wrote:

> > > > > VALUE block = rb\_proc\_new(…);
> 
> and I need  
> method(object) { some\_code}

Right, stupid me. Should have looked more closely. My apologies for  
the unnecessary noise.

rb\_iterate seems indeed the proper method:

[http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext\_ruby.html#S8](http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html#S8)

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![peterz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@peterz](https://www.ruby-forum.com/u/peterz)\
**Post date:** [March 29, 2009, 8:57pm UTC](https://www.ruby-forum.com/t/passing-block-in-c/164178/9 "2009-03-29T20:57:51Z")

</div>

Quoting “Robert K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > > > >
> > > 
> > > [http://www.google.com/codesearch/p?hl=de#X2brleciKjw/trunk/devel/RubyClass.cpp&q=rb\_proc\_new](http://www.google.com/codesearch/p?hl=de#X2brleciKjw/trunk/devel/RubyClass.cpp&q=rb_proc_new)  
> > > method(object, proc { some\_code })  
> > > and I need  
> > > method(object) { some\_code}
> 
> Right, stupid me. Should have looked more closely. My apologies  
> for the unnecessary noise.
> 
> rb\_iterate seems indeed the proper method:
> 
> [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html#S8)

Yes, rb\_iterate is exactly what I need.  
Thanks for this ruby-doc link anyway… before I analyzed sources of  
every un-obvious function 🙂

WBR, Peter Z.
