[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Passing block in C

Peter Zotov

3/29/2009 2:31:00 PM

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

8 Answers

Phlip

3/29/2009 2:57:00 PM

0

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=r...

Peter Zotov

3/29/2009 3:58:00 PM

0

Quoting Phlip <phlip2005@gmail.com>:

> 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=r...

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

Robert Klemme

3/29/2009 4:14:00 PM

0

On 29.03.2009 17:58, Peter Zotov wrote:
> Quoting Phlip <phlip2005@gmail.com>:
>
>> 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=r...
>
> 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=r...

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

Cheers

robert

Peter Zotov

3/29/2009 4:24:00 PM

0

Quoting "Robert Klemme" <shortcutter@googlemail.com>:

> On 29.03.2009 17:58, Peter Zotov wrote:
>> Quoting Phlip <phlip2005@gmail.com>:
>>
>>> Peter Zotov wrote:
>>>
>>>> How can I call Ruby function from C with block?
>>>> E. g.
>>>>
>>>> VALUE block =3D 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=3Dr...
>>
>> Ehm. I meant 'what function must be in place of =20
>> rb_funcall_with_block'. I _know_ how do rb_proc_new work.
>
> 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/deve...
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

Andre Nathan

3/29/2009 4:53:00 PM

0

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


Phlip

3/29/2009 5:44:00 PM

0

> I think that Phlip understood this.

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

Robert Klemme

3/29/2009 5:52:00 PM

0

On 29.03.2009 18:23, Peter Zotov wrote:
> Quoting "Robert Klemme" <shortcutter@googlemail.com>:
>
>> On 29.03.2009 17:58, Peter Zotov wrote:
>>> Quoting Phlip <phlip2005@gmail.com>:
>>>
>>>> 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=r...
>>> 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=r...
>>
>> 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}

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_ru...

Kind regards

robert

Peter Zotov

3/29/2009 6:57:00 PM

0

Quoting "Robert Klemme" <shortcutter@googlemail.com>:

> On 29.03.2009 18:23, Peter Zotov wrote:
>> Quoting "Robert Klemme" <shortcutter@googlemail.com>:
>>
>>> On 29.03.2009 17:58, Peter Zotov wrote:
>>>> Quoting Phlip <phlip2005@gmail.com>:
>>>>
>>>>> Peter Zotov wrote:
>>>>>
>>>>>> How can I call Ruby function from C with block?
>>>>>> E. g.
>>>>>>
>>>>>> VALUE block =3D 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=3Dr...
>>>> Ehm. I meant 'what function must be in place of =20
>>>> rb_funcall_with_block'. I _know_ how do rb_proc_new work.
>>> 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/de...
lass.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}
>
> Right, stupid me. Should have looked more closely. My apologies =20
> for the unnecessary noise.
>
> rb_iterate seems indeed the proper method:
>
> http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_ru...

Yes, rb_iterate is exactly what I need.
Thanks for this ruby-doc link anyway... before I analyzed sources of =20
every un-obvious function :)


WBR, Peter Zotov