From: Ryan Davis Date: 2007-07-17T07:53:25+09:00 Subject: Re: C library extension question On Jul 16, 2007, at 12:20 , Oliver wrote: > /* 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; What you're looking for is probably Data_Wrap_Struct and friends. See ruby.h and intern.h. For a simple example of this in action, look at the image_science.rb code (available via gems or on the seattlerb project on rubyforge). It is only 228 lines long currently so it is a pretty easy read. > Then, as a user you can define an array of function pointers like > this: > > proxy_cmd cmds[] = { > func_a, > func_b, > func_c, > ... > }; > > and fill in the structure: > > proxy_commands mytabs = { > CMD_BASE; > sizeof(cmds)/sizeof(proxy_cmd), > cmds > } I'd suggest using a ruby array at this stage. Cleaner. > An example of API will look like this: > register(&mytabs, .... ) > > What is the best way of wrapping this in Ruby? Well, the BEST way is to not wrap it at all: def func_a; ...; end def func_b; ...; end msgs = [:func_a, :func_b] msgs.each do |msg| send msg end Short of that, you'll want to poke around with DataWrapStruct.