From: Edward Liu Date: 2008-01-15T00:59:05+09:00 Subject: Re: Using ruby.h to generate C++ vectors and maps I'm looking through the documentation for ruby.h and intern.h, and I am having a problem with trying to be able to access a Ruby hash inside a C/C++ program. I see that using ruby.h, I have access to rb_hash_new and a couple other hash functions. However, it doesn't seem that I can use a function that will return me an array of keys to access the hash. For example, hash.c in Ruby has a rb_hash_keys functions. However, it is not accessible through either ruby.h or intern.h. Does anyone know of any way I can full access to the Ruby hash and array functions for C/C++? Thanks! Edward Jason Roelofs wrote: > On Jan 11, 2008 2:51 PM, Edward Liu wrote: > >> > >> >> if(rb_obj_is_instance_of(object, "Hash") { >> >> -- >> 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 vec; > > for(int i = 0; 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 -- Posted via http://www.ruby-forum.com/.