From: Ville Sipola Date: 2011-05-07T04:47:05+09:00 Subject: Re: Fast way to move C array to ruby (rb_ary_new4)? Ville Sipola wrote: > Now I only have to figure out how to use that. Now that I have, I thought it a good idea to describe how it was done, so that others reading the post might be able to use the information. So this was my method. The writing part is a bit of a hack. Masahiro Tanaka himself advices against this approach in here: https://www.ruby-forum.com/topic/83484, but I'm yiet to find a better solution. If there is a function for writing c data in an narray in a safe way, I'd like to hear about it. First, in Ruby I did something like @x=NArray.float(@number_of_samples).fill(some_value) to make sure that the narray is of correct size. Then in C: static VALUE writingfunction(VALUE class,VALUE x) { struct NARRAY *xarray; GetNArray(x, xarray); double *floatpointerx = xarray->ptr; double c_value; for (counter = 0; counter < number_of_samples; counter++) { (do the actual calculation) floatpointerx[samplecounter] = c_value; (This is the illegal hack) } } To read in C (this I believe is completely ok, please correct if I'm wrong.) static VALUE readingfunction(VALUE class, VALUE x) { struct NARRAY *xarray; GetNArray(x, xarray); double *floatpointerx = xarray->ptr; for (counter = 0; counter < xarray->total; counter++) { a_function_that_takes_c_double(floatpointerx[counter1]); } } -- Posted via http://www.ruby-forum.com/.