Brian Schröäer wrote: > Hello Group, > > I'm asking myself what is the way to create a ruby object correctly from > a C extension. As I've extracted from ruby.h and README.EXT the things I > can come up with are: > > rb_funcall(rb_const_get(self, rb_intern('Complex')), rb_intern('new'), 2, rb_float_new(real), rb_float_new(imag)); > // which is impressively complicated and long. You could write a function like /* initialize these static variables in your *_init() function */ static VALUE cComplex; static ID id_new; static VALUE complex_new(double real, double imag) { return rb_funcall(cComplex, id_new, 2, rb_float_new(real), rb_float_new(imag)); } Since the complex class is implemented in ruby there is no rb_complex_new(). > > rb_eval_string(constructing_string) > // Which is ugly because of eval and because of C string functions. You can use the ruby string functions and then call StringValueCStr(ruby_string). > > This seems to be really complicated just for wrapping my C complex > numbers into ruby Complex numbers. I think it would be nice if ruby had a COMPLEX_T - for both efficency and for writing C extensions. On 64 bit systems it is easy to add this, but on 32 bit systems it would increase the size of all ruby objects unless you use a pointer to a second structure. But, considering that even C has a built in complex type, I think it would be worth while. > > So as I always have seen that things are easier in ruby, what should I > do? > Use ruby ;) -Charlie