I am trying to pass a ruby array from one C function to another but I keep getting the following error
error: too few arguments to function ‘c_sum’
here is my code
require 'inline'
class ArrayMath
inline do |builder|
builder.c_singleton "
static VALUE c_sum(VALUE arr){
double result = 0;
long i, len = RARRAY_LEN(arr);
VALUE *c_arr = RARRAY_PTR(arr);
for(i=0; i<len; i++) {
result += NUM2DBL(c_arr[i]);
}
return rb_float_new(result);
}"
builder.c_singleton "
static VALUE c_avg(VALUE arr){
double sum, result, len = RARRAY_LEN(arr);
// c_sum returns a ruby float
sum = NUM2DBL(c_sum(arr));
result = sum / len;
return rb_float_new(result);
}"
end
class << self
alias sum c_sum
alias avg c_avg
end
end
I get the error while trying
ArrayMath.avg([2,3.4,5.24])
I decided to change things a bit. Here is how I got things to work.
I created a file called array_math.rb in /lib folder (I use it in a rails application) which gets initialized from application.rb with
here is array_math.rb
Now we can do this: