From: Tim Pease Date: 2007-08-08T00:12:27+09:00 Subject: Re: [C ext to Ruby] how to scan a RARRAY ??? On 8/7/07, unbewust wrote: > from ruby.h : > > struct RArray { > struct RBasic basic; > long len; > union { > long capa; > VALUE shared; > } aux; > VALUE *ptr; > }; > > > my module make use of : > VALUE m_set_icon(VALUE self, VALUE src_path, VALUE dst_pathes[]) > VALUE m_set_icon(VALUE self, VALUE src_path, VALUE dst_pathes) > dst_pathes is a T_ARRAY > > int len = RARRAY(dst_pathes)->len; > printf("len = %d\n", len); > char *cdst_pathes[len]; > > how to duplicated the VALUE (shared) from the struct RArray into > cdst_pathes (an Array of char *) ??? > > int len = RARRAY(dst_pathes)->len; char* cdst_pathes[len]; long ii; for (ii = 0; ii < len; ii++) { VALUE str = rb_ary_entry( dst_pathes, ii ); cdst_pathes[ii] = StringValueCStr( str ); } rb_ary_entry will return the VALUE from the Array at the given index. StringValueCStr will check that the given VALUE is a String, and then it will return the pointer of that String. This will not give you copy, so the memory is still owned by the Ruby interpreter, and it will be garbage collected if the original String object goes out of scope. You might need to use memcopy to get a copy of the string that will not be garbage collected -- just depends on how long you're going to need a reference to the data. Take a look at the Ruby source code for more methods for working with Arrays and Strings. ruby.h and intern.h rb_ary_ <-- methods that operate on Array objects rb_string_ <-- methods that operate on String objects Blessings, TwP