# Ruby callback for a SWIG wrapped C function

**URL:** <https://www.ruby-forum.com/t/ruby-callback-for-a-swig-wrapped-c-function/146272>\
**Category:** Ruby\
**Created:** [August 19, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/ruby-callback-for-a-swig-wrapped-c-function/146272 "2008-08-19T17:43:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [August 19, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/ruby-callback-for-a-swig-wrapped-c-function/146272/1 "2008-08-19T17:43:02Z")

</div>

Hello list!

I successfully wrapped using swig a library I wrote in ruby, all its  
simple  
functions works very well.  
I said simple because I have a function that takes a callback function  
as  
argument, and I look for the way how to give a pure ruby callback  
function  
as argument of this function wrapped by SWIG!

By the way, may I meet problems if some of my functions uses the  
“callback-needing” function inside of them?

Than kyou

---

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [August 20, 2008, 1:12pm UTC](https://www.ruby-forum.com/t/ruby-callback-for-a-swig-wrapped-c-function/146272/2 "2008-08-20T13:12:21Z")

</div>

Lyes A. wrote:

> I said simple because I have a function that takes a callback function as  
> argument, and I look for the way how to give a pure ruby callback function  
> as argument of this function wrapped by SWIG

You haven’t supplied enough detail to provide specific advice so you’ll  
need to adapt what follows. A generally effective way of dealing with  
this is in SWIG to do something like:

1. Create a short ‘bridging’ function in C that converts arguments to  
ruby and then calls rb\_yield

Let’s say your callback function is expected to take a single int  
argument and returns void. Put this is in as a SWIG literal in your .i  
file.

%{  
void DoYielding(int arg\_1)  
{  
VALUE rb\_arg\_1 = INT2NUM(arg\_1);  
rb\_yield(arg\_1);  
}  
%}

If the callback needs to return a value, you’ll need to capture the  
return value of rb\_yield and do some Ruby -\> C type translation.

1. Set it up so that method calls in ruby to the original  
callback-needing function supply this callback as the C argument

If the callback-needing function needs to accept a single int parameter,  
the method that is exposed in ruby will look something like

VALUE method\_needing\_callback(VALUE rb\_input\_arg)  
{  
int input\_arg = NUM2INT(rb\_input\_arg);  
C\_Function\_Needing\_Callback(input\_arg, &DoYielding);  
return Qnil;  
}

Then do whatever you need to get this function mapped to a ruby method,  
using normal SWIG techniques.

1. Compile, and call the method with a block

some\_object.method\_needing\_callback(42) do | an\_int |  
puts “The callback was called with argument %i” % an\_int  
end

> By the way, may I meet problems if some of my functions uses the  
> “callback-needing” function inside of them?

Not if you do it right. You can still call the callback-needing function  
with a normal callback, written in pure C/C++.

a

---

<div class="post-metadata">

**Author:** ![lyes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ed8c4c/32.png) [@lyes](https://www.ruby-forum.com/u/lyes)\
**Post date:** [August 20, 2008, 3:16pm UTC](https://www.ruby-forum.com/t/ruby-callback-for-a-swig-wrapped-c-function/146272/3 "2008-08-20T15:16:02Z")

</div>

On Wed, Aug 20, 2008 at 12:06 PM, Alex F.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> rb\_yield(arg\_1);  
> the method that is exposed in ruby will look something like
> 
> Not if you do it right. You can still call the callback-needing function  
> with a normal callback, written in pure C/C++.
> 
> a

Thank tou for the Reply, I’ll try what you suggested me. If I have more  
troubles, I will repost another message
