# RubyInline - passing pointer from one function to another

**URL:** <https://www.ruby-forum.com/t/rubyinline-passing-pointer-from-one-function-to-another/179255>\
**Category:** Ruby\
**Created:** [November 20, 2009, 3:27pm UTC](https://www.ruby-forum.com/t/rubyinline-passing-pointer-from-one-function-to-another/179255 "2009-11-20T15:27:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@Marcin\_L](https://www.ruby-forum.com/u/Marcin_L)\
**Post date:** [November 20, 2009, 3:27pm UTC](https://www.ruby-forum.com/t/rubyinline-passing-pointer-from-one-function-to-another/179255/1 "2009-11-20T15:27:44Z")

</div>

Hi,

I’m trying to write ruby interface to JACK audio library. I’ve chosen  
RubyInline due it’s simplicity. What I want to achieve is to have ruby  
code like this:

client = JACK::Client.new “my\_name”  
puts client.get\_sample\_rate

To achieve that I need to pass pointer to variable of jack\_client\_t\*  
from libjack from one function to another. What I tried to do is  
available at [http://pastebin.com/m3b6ee0c9](http://pastebin.com/m3b6ee0c9) but as you can expect it  
doesn’t work.

How can I do that? Is there any way to save pointer in instance of  
ruby class e.g. in the same way as I can access strings?

Thanks in advance,

m.

---

<div class="post-metadata">

**Author:** ![Marcin\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@Marcin\_L](https://www.ruby-forum.com/u/Marcin_L)\
**Post date:** [November 20, 2009, 6:15pm UTC](https://www.ruby-forum.com/t/rubyinline-passing-pointer-from-one-function-to-another/179255/2 "2009-11-20T18:15:25Z")

</div>

Marcin L. wrote:

> I’m trying to write ruby interface to JACK audio library. I’ve chosen  
> RubyInline due it’s simplicity.

I’m afraid I’m not answering your question, but have you looked at FFI?  
This is designed to do exactly what you want, namely, calling an  
existing C library from Ruby.

---

<div class="post-metadata">

**Author:** ![Marcin\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@Marcin\_L](https://www.ruby-forum.com/u/Marcin_L)\
**Post date:** [November 20, 2009, 7:17pm UTC](https://www.ruby-forum.com/t/rubyinline-passing-pointer-from-one-function-to-another/179255/3 "2009-11-20T19:17:45Z")

</div>

Hi,

seems that could be helpful. I’ll check FFI.

m.

---

<div class="post-metadata">

**Author:** ![Marcin\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@Marcin\_L](https://www.ruby-forum.com/u/Marcin_L)\
**Post date:** [November 24, 2009, 12:18am UTC](https://www.ruby-forum.com/t/rubyinline-passing-pointer-from-one-function-to-another/179255/4 "2009-11-24T00:18:47Z")

</div>

On Nov 20, 2009, at 06:26, Marcin L. wrote:

> doesn’t work.
> 
> How can I do that? Is there any way to save pointer in instance of  
> ruby class e.g. in the same way as I can access strings?

You’ll need to use the Ruby/C API for this, namely Data\_Wrap\_Struct  
and Data\_Get\_Struct. Passing around the pointer as an instance  
variable (without even using INT2FIX/FIX2INT) can only lead to memory  
leaks and segfaults.

If you’re using RubyInline, place everything in the same inline block  
per class. Use private as a method afterward:

class JACK::Client  
inline do |builder|  
builder.c \<\<-C  
/\* define connect \*/  
C  
end

private :connect  
end

There are several strategies to wrapping C libraries in a class so  
they are initialized properly.

This uses ::allocate:

> <https://github.com/drbrain/ffmpeg-rb/blob/master/lib/ffmpeg/fifo.rb>

This uses :🆕

> <https://github.com/drbrain/ffmpeg-rb/blob/master/lib/ffmpeg/frame_buffer.rb>

This uses a custom method ::for\_decoder:

> <https://github.com/drbrain/ffmpeg-rb/blob/master/lib/ffmpeg/codec.rb>

Also, your extension doesn’t make much use of RubyInline’s  
helpfulness. RubyInline will automatically perform input and output  
casts for you. Connect should be a class method written like:

builder.c\_singleton \<\<-C  
static VALUE connect(char \*name) {  
jack\_client\_t \*client;

```
 client = jack_client_new(name);

 if (client == 0)
   rb_raise(rb_eRuntimeError, "cannot connect to JACK server");

 /* guessing at free function */
 return Data_Wrap_Struct(self, NULL, jack_client_close, client);

```

}  
C

With jack\_get\_sample\_rate bound like:

builder.c \<\<-C  
static jack\_nframes\_t sample\_rate() { /\* rubyish method name \*/  
jack\_client\_t \*client;

```
 Data_Get_Struct(self, jack_client_t, client);

 return jack_get_sample_rate(client);

```

}  
C

and a type converter:

builder.alias\_type\_converter ‘unsigned long’, ‘jack\_nframes\_t’
