Forum: Ruby Passing block in C

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2a2ef2adb5f48cfec60ba3c3b381eead?d=identicon&s=25 Peter Zotov (Guest)
on 2009-03-29 16:31
(Received via mailing list)
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
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-29 17:01
(Received via mailing list)
Peter Zotov 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
2a2ef2adb5f48cfec60ba3c3b381eead?d=identicon&s=25 Peter Zotov (Guest)
on 2009-03-29 17:58
(Received via mailing list)
Quoting Phlip <phlip2005@gmail.com>:

>   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 Zotov
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-03-29 18:15
(Received via mailing list)
On 29.03.2009 17:58, Peter Zotov wrote:
>>
>>   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#X2brleciK...

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

Cheers

  robert
2a2ef2adb5f48cfec60ba3c3b381eead?d=identicon&s=25 Peter Zotov (Guest)
on 2009-03-29 18:24
(Received via mailing list)
Quoting "Robert Klemme" <shortcutter@googlemail.com>:

>>> 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 Zotov
8b4249ca3bb8c123da9f7aca63a652e1?d=identicon&s=25 Andre Nathan (Guest)
on 2009-03-29 18:53
(Received via mailing list)
Hi Peter,

On Sun, 2009-03-29 at 23:30 +0900, Peter Zotov 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
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-29 19:47
(Received via mailing list)
> I think that Phlip understood this.

Actually I didn't. I was just having a "let me Google CodeSearch that
for you"
moment... (-:
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-03-29 19:55
(Received via mailing list)
On 29.03.2009 18:23, Peter Zotov 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/...

Kind regards

  robert
2a2ef2adb5f48cfec60ba3c3b381eead?d=identicon&s=25 Peter Zotov (Guest)
on 2009-03-29 20:57
(Received via mailing list)
Quoting "Robert Klemme" <shortcutter@googlemail.com>:

>>>>>>
>>> 
http://www.google.com/codesearch/p?hl=de#X2brleciK...
>>   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/...

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 Zotov
This topic is locked and can not be replied to.