Passing block in C

P

Peter Zotov

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 Zotov
 
P

Peter Zotov

Quoting "Robert Klemme said:
I think that Phlip understood this. If you look at the results =20
you'll find this

http://www.google.com/codesearch/p?hl=3Dde#X2brleciKjw/trunk/devel/RubyCla= ss.cpp&q=3Drb_proc_new

Which proves that his suggested search was indeed helpful. :)

It isn't. See RubyDoc:

ObjectSpace.define_finalizer(obj, aProc=3Dproc())
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 Zotov
 
A

Andre Nathan

Hi Peter,

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
 
P

Phlip

I think that Phlip understood this.

Actually I didn't. I was just having a "let me Google CodeSearch that for you"
moment... (-:
 
R

Robert Klemme

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}

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

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top