c - How do I handle ruby arrays in ruby ffi gem? -
i want use ruby ffi gem call c function has array input variable , output array. is, c function looks like: double *my_function(double array[], int size) i have created ruby binding as: module mymodule extend ffi::library ffi_lib 'c' ffi_lib 'my_c_lib' attach_function :my_function, [:pointer, int], :pointer i make call in ruby code like: result_array = mymodule.my_function([4, 6, 4], 3) how go this? let's library wish use in ruby script, call my_c_lib.c : #include <stdlib.h> double *my_function(double array[], int size) { int = 0; double *new_array = malloc(sizeof(double) * size); (i = 0; < size; i++) { new_array[i] = array[i] * 2; } return new_array; } you can compile so: $ gcc -wall -c my_c_lib.c -o my_c_lib.o $ gcc -shared -o my_c_lib.so my_c_lib.o now, it's ready use in in ruby code ( my_c_lib.rb ): require 'ffi' module mymodule extend ffi::library # assuming library files in s...