From: Oliver Date: 2007-07-17T11:55:36+09:00 Subject: ruby extension help hi, folks - I am working on a ruby extension for an existing C library, and ran into difficulties due to a "unique" data structure (unique in the sense that I didn't find any examples to follow). I've posted a similar help message on comp.language.ruby, but didn't get a clear answer, hopefully this will reach a wider developer community and I appreciate your help. === background The C library I am working on has a so-called dynamic command dispatch structure, it is basically a hook meant for accommodating a set of user-provided call back functions. It is defined as follows: /* command dispatch structure */ typedef int (*proxy_cmd)(int trans_id, int nargs, char **args); struct proxy_commands { int cmd_base; int cmd_size; proxy_cmd * cmd_funcs; }; typedef struct proxy_commands proxy_commands; /* end of definition */ === C usage Under normal case, a C library user will supply the following structure and make use of above structure. int my_init(server,port) { ... } int my_connect( ... ) { ...} /* put all function pointer into array */ proxy_cmd cmds[] = { my_init, my_connect, my_finish, ... }; /* and then fill in the required structure */ proxy_commands mytabs = { CMD_BASE; sizeof(cmds)/sizeof(proxy_cmd), cmds } /* finally, they can call a library API to register these functions */ register(&mytabs) === Ruby extension Now, when I try to wrap above structure in ruby, I am facing a dilemma: for the call back function that a C user need to supply, I want the flexibility to allow user to write those call back functions in Ruby. Otherwise, they will have to write a mixture of ruby and C to use a C library, which is kind of awkward. To support writing these call backs in Ruby, that means in the wrapper code, I need to "map" those Ruby functions into C, somehow, and fill in the required structure such as I did in "mytabs", is this correct steps to follow? and how do I do that? I am hoping ruby code of using the library will be something like this, but I don't know if it is possible at all: def my_init(server, port) { ... } end def my_connect(...) { ... } end funcs = [my_init, my_connect] foo = Proxy.new foo.register(funcs) or something like that. I know this is a convoluted case, and probably too much details to follow. Let me know if this needs any clarification. Any pointer or help is much appreciated. Thanks Oliver