# Passing C Struct to Ruby

**URL:** <https://www.ruby-forum.com/t/passing-c-struct-to-ruby/181898>\
**Category:** Ruby\
**Created:** [January 19, 2010, 10:09pm UTC](https://www.ruby-forum.com/t/passing-c-struct-to-ruby/181898 "2010-01-19T22:09:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![coldpeace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f4b2a3/32.png) [@coldpeace](https://www.ruby-forum.com/u/coldpeace)\
**Post date:** [January 19, 2010, 10:09pm UTC](https://www.ruby-forum.com/t/passing-c-struct-to-ruby/181898/1 "2010-01-19T22:09:54Z")

</div>

hey !!

I’m newbie in Ruby ,  
Actually , I wanna call some C methods and Structure from Ruby , But  
Still dont knw how to do

in C …

int VALUE my\_str\_new(VALUE arg)  
{  
my\_struct \*str =init\_Struct(arg) ;  
return \*str ;  
}  
…?!

I dont know how to tell Ruby about my C struct and its methods( or  
create equivalent struct (keeping the C methods ) in Ruby)

Thx

---

<div class="post-metadata">

**Author:** ![coldpeace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f4b2a3/32.png) [@coldpeace](https://www.ruby-forum.com/u/coldpeace)\
**Post date:** [January 19, 2010, 10:19pm UTC](https://www.ruby-forum.com/t/passing-c-struct-to-ruby/181898/2 "2010-01-19T22:19:46Z")

</div>

> I dont know how to tell Ruby about my C struct and its methods( or  
> create equivalent struct (keeping the C methods ) in Ruby)

checkout ffi, ruby ffi swig generator, and

> **[GitHub - remogatto/ffi-inliner: Mix C in (J)Ruby and gulp it on the fly!](https://github.com/remogatto/ffi-inliner)**
>
> Mix C in (J)Ruby and gulp it on the fly! Contribute to remogatto/ffi-inliner development by creating an account on GitHub.

either that or if you want to write a c extension google for “ruby c  
extension”

-r

---

<div class="post-metadata">

**Author:** ![coldpeace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f4b2a3/32.png) [@coldpeace](https://www.ruby-forum.com/u/coldpeace)\
**Post date:** [January 19, 2010, 10:25pm UTC](https://www.ruby-forum.com/t/passing-c-struct-to-ruby/181898/3 "2010-01-19T22:25:46Z")

</div>

El Martes, 19 de Enero de 2010, Mido P. escribiÃ³:

> in C …
> 
> int VALUE my\_str\_new(VALUE arg)  
> {  
> my\_struct \*str =init\_Struct(arg) ;  
> return \*str ;  
> }

This is wrong. Any Ruby C function must return a VALUE object. This is,  
a Ruby  
method cannot return a C integer or a C struct.

So you need to translate the above C struct into a Ruby hash, Ruby  
struct or a  
custom Ruby class instance.

But definitively I recommend you to look for Ruby C extensions  
tutorials.
