[prev in list] [next in list] [prev in thread] [next in thread]
List: ruby-talk
Subject: [ruby-talk:00150] Re: calling a Ruby function from C?
From: shugo () aianet ! ne ! jp (Shugo Maeda)
Date: 1998-12-27 14:51:30
[Download RAW message or body]
Hi, Bryce,
In message "[ruby-talk:00146] calling a Ruby function from C?"
"Bryce" <crowdog@siscom.net> wrote:
|What I am wanting to accomplish is like a callback.
|
|I would like to send my C code a Ruby function pointer
|so that C can call my Ruby function.
You can use a Method object to pass a Ruby function to C function.
C:
VALUE c_func(VALUE self, VALUE func)
{
...
rb_funcall(func, rb_intern("call"), 0);
...
}
Ruby:
def foo
puts "foo"
end
c_func(method(:foo))
And you can also pass a Proc object to c_func.
p = proc { puts "foo" }
c_func(p)
But it may be better way to define an iterator.
-- The name `iterator' may be obsolete.
C:
VALUE c_func2(VALUE self)
{
...
rb_yield(Qnil);
...
}
Ruby:
def foo
puts "foo"
end
c_func2 { foo }
You can also pass a method object to c_func2:-)
like this:
c_func(&method(:foo))
& means passing an argument as an iterator block.
Hope this helps, Salut!
Shugo
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic