Using ruby.h to generate C++ vectors and maps

Discussion in 'Ruby' started by Edward Liu, Jan 11, 2008.

  1. Edward Liu

    Edward Liu Guest

    I am trying to use ruby.h to extend Ruby with C/C++. I understand how
    to deal with numbers and strings, but I am having trouble finding
    documentation about vectors and maps. Let's say I have this function
    for example:

    VALUE read_values(VALUE self, VALUE object);

    The 'object' variable can represent either a Ruby array or a Ruby hash.
    If it is a Ruby array, I want to translate it to a vector. If it is a
    Ruby hash, I want to translate it to a map. I know for integers, there
    is a macro called NUM2INT that converts a Ruby number into a C/C++
    integer. For strings, I know that the function StringValueCStr does
    what I want. I couldn't find it in the ruby.h documentation, but is
    there any similar functionality in ruby.h for translating to vectors and
    maps? If so, what is the functionality? If there is not a direct
    translation, what would be the best approach to do my task? I'd like to
    avoid using another protocol, like SWIG, for this task, if possible.

    Thanks,
    Edward
    --
    Posted via http://www.ruby-forum.com/.
    Edward Liu, Jan 11, 2008
    #1
    1. Advertising

  2. [Note: parts of this message were removed to make it a legal post.]

    On Jan 11, 2008 2:12 PM, Edward Liu <> wrote:

    > I am trying to use ruby.h to extend Ruby with C/C++. I understand how
    > to deal with numbers and strings, but I am having trouble finding
    > documentation about vectors and maps. Let's say I have this function
    > for example:
    >
    > VALUE read_values(VALUE self, VALUE object);
    >
    > The 'object' variable can represent either a Ruby array or a Ruby hash.
    > If it is a Ruby array, I want to translate it to a vector. If it is a
    > Ruby hash, I want to translate it to a map. I know for integers, there
    > is a macro called NUM2INT that converts a Ruby number into a C/C++
    > integer. For strings, I know that the function StringValueCStr does
    > what I want. I couldn't find it in the ruby.h documentation, but is
    > there any similar functionality in ruby.h for translating to vectors and
    > maps? If so, what is the functionality? If there is not a direct
    > translation, what would be the best approach to do my task? I'd like to
    > avoid using another protocol, like SWIG, for this task, if possible.
    >
    > Thanks,
    > Edward
    > --
    > 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
    Jason Roelofs, Jan 11, 2008
    #2
    1. Advertising

  3. Edward Liu

    Edward Liu Guest

    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 <> 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/.
    Edward Liu, Jan 11, 2008
    #3
  4. Edward Liu

    Edward Liu Guest

    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 <> 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/.
    Edward Liu, Jan 11, 2008
    #4
  5. [Note: parts of this message were removed to make it a legal post.]

    On Jan 11, 2008 2:51 PM, Edward Liu <> 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 <> 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 = 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
    Jason Roelofs, Jan 11, 2008
    #5
  6. Edward Liu

    Edward Liu Guest

    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<your_type> 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/.
    Edward Liu, Jan 14, 2008
    #6
  7. Edward Liu

    Andre Nathan Guest

    On Tue, 2008-01-15 at 00:59 +0900, Edward Liu wrote:
    > Does anyone know of any way I can full access to the Ruby
    > hash and array functions for C/C++?


    You can use rb_funcall():

    VALUE hash = ...;
    VALUE keys = rb_funcall(hash, rb_intern("keys"), 0);

    HTH,
    Andre
    Andre Nathan, Jan 14, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Elliott
    Replies:
    4
    Views:
    1,146
    Simon Elliott
    Mar 10, 2005
  2. Sheep
    Replies:
    1
    Views:
    383
    Sheep
    Aug 14, 2006
  3. LeTubs
    Replies:
    1
    Views:
    280
    Jonathan Mcdougall
    Dec 5, 2005
  4. Marcus
    Replies:
    2
    Views:
    579
    Marcus
    Dec 9, 2005
  5. Guest
    Replies:
    0
    Views:
    429
    Guest
    Sep 14, 2005
Loading...

Share This Page