------art_3259_3217344.1200082570854
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Jan 11, 2008 2:51 PM, Edward Liu <liu_edward / emc.com> wrote:

> Also, when I do the translation, I don't want to use functions like
> rb_ary_new2 or rb_hash_new, since these two functions both return VALUE
> objects, not std::vector or std::map objects.  I know there is no direct
> function in ruby.h to do the translation.  I'm wondering if this is a
> certain process of function calls in ruby.h to do this.  Thanks!
>
> Edward Liu wrote:
> > I already know how to check if 'object' is an array or a hash.  I am
> > looking for how to translate 'object' from a Ruby array to a C/C++
> > vector or from a Ruby hash to a C/C++ map.
> >
> > Jason Roelofs wrote:
> >> On Jan 11, 2008 2:12 PM, Edward Liu <liu_edward / emc.com> wrote:
> >>
> >>> is a macro called NUM2INT that converts a Ruby number into a C/C++
> >>> Posted via http://www.ruby-forum.com/.
> >>>
> >>>
> >> You'll want something like:
> >>
> >> VALUE read_values(VALUE self, VALUE object) {
> >>    if(rb_obj_is_instance_of(object, "Hash") {
> >>
> >>   } else if (rb_obj_is_instance_of(object, "Array") {
> >>
> >>   }
> >> }
> >>
> >> API is best found here:
> http://www.rubycentral.com/pickaxe/ext_ruby.html
> >>
> >> Jason
>
> --
> Posted via http://www.ruby-forum.com/.
>
>
Then no, there isn't. If you know that your array / hash is full of a single
type, then the following will work fine:

std::vector<your_type> vec;

for(int i  ; i < RARRAY(object)->len; i++) {
  vec << NUM2INT(rb_array_get(i)); // or STR2CSTR, whatever conversion is
needed
}

But if it's a generic array and you can't guarentee types, you'll have to
make some concessions (like turning the values into strings).

Look through the API, there are methods and macros that will help you.

Jason

------art_3259_3217344.1200082570854--