[MacRuby-devel] Static C-Arrays

Laurent Sansonetti lsansonetti at apple.com
Thu Sep 10 12:54:38 PDT 2009


Hi Tom,

In MacRuby, an Array is actually a NSMutableArray instance, as you may  
know.

If you need to call a C or Objective-C method that accepts a C-style  
array, you can just pass the regular array object and MacRuby should  
do the conversion for you.

If you want to build the C array by yourself in pure Ruby, you might  
want to look at the Pointer class.

irb(main):001:0> ptr = Pointer.new(:int, 5)
=> #<Pointer:0x200337640>
irb(main):002:0> 5.times { |i| ptr[i] = i }
=> 5

This creates a C-style array of 5 C integers. You can access the slots  
by using #[] and #[]=. If you pass this pointer to a C or Objective-C  
method that is supposed to accept a C-style array of C integers, it  
should work.

Laurent

On Sep 10, 2009, at 9:37 AM, Tom Kleber wrote:

> Is it possible to create static c-arrays in macruby?
> In jruby Array has a a #to_java method which converts a Array object
> to an java array (a = Array.new(6).to_java(:double))
> Is there something like that supported in macruby?
>
> Tom
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel



More information about the MacRuby-devel mailing list