Subject:
[ruby-ffi] Re: Array of Ints
From:
John Lee
Date:
10/26/11 10:31 AM
To:
ruby-ffi

I tried wrapping my code inside a module, but the problem still
persists.

Do you think :pointer is the correct type to represent the array in
attach_function.

I know that if you have an array in a struct, you can specify it as:
class Struct1 < ::FFI::Struct
   layout :value1, [:int, 3]
end

So I tried to replace :pointer with [:int, 3] in attach_function, but
it complains about "unable to resolve type ':int, 3]' <TypeError>"
attach_function :function1, [[:int, 3], :int, :int], :int

Has anyone got arrays to work with FFI?

On Oct 25, 4:25 pm, Chuck Remes <cremes.devl...@mac.com> wrote:
> On Oct 25, 2011, at 3:51 PM, John Lee wrote:
>
>
>
>
>
>
>
>
>
> > I am  using Ruby 192
>
> > I have the following c function:
>
> > int function1(int *dataArray, int value1 , int value2)
> > The first argument is an array of ints
>
> > Here is my FFI code:
> > attach_function :function1, [:pointer, :int, :int], :int
> > ruby_array =  FFI::MemoryPointer.new(:int,4)
> > ruby_array.write_array_of_int(1000,2000,3000,4000])
>
> > ...
> > function1(ruby_array, 1, 2)
>
> > But that produces an error message that function1 is an  undefined
> > method.
>
> Try wrapping your FFI code up in a module.
>
> e.g.
> module MyLibrary
>   ffi_libs(path_to_lib)
>
>   attach_function :function1, [:pointer, :int, :int], :int
> end
>
> MyLibrary.function1(ruby_array, 1, 2)
>
> cr