From: Rob Biedenharn Date: 2007-01-12T00:41:11+09:00 Subject: Re: Sharing constants between C and Ruby code On Jan 11, 2007, at 10:01 AM, gwtmp01@mac.com wrote: > > On Jan 11, 2007, at 9:04 AM, Rob Biedenharn wrote: >> rb_define_const(mMyConstants, "MY_CONSTANT", INT2NUM >> (MY_CONSTANT)); >> rb_define_const(mMyConstants, "YOUR_CONSTANT", INT2NUM >> (YOUR_CONSTANT)); >> rb_define_const(mMyConstants, "HIS_CONSTANT", INT2NUM >> (HIS_CONSTANT)); > > Hah! I wasn't too far off with my guess of: >> rb_constant_set( klass, 'MY_CONSTANT', MY_CONSTANT) > > > Anyway, you are still stuck with having to keep the C .h file and > the Ruby > .c extension in sync by hand. I'd pick any other scheme that > allowed you > to only worry about maintaining a single file with the constant > definitions. > > Gary Wright It depends on whether the OP is adding new constants to the file (#define HER_CONSTANT 105) or just changing the values (#define MY_CONSTANT 42). You could always generate the .c file from the .h if that were a problem -- and that's what others suggested doing from .h to .rb directly. This solution however, lets the C preprocessor and/or compiler do the dirty work of parsing the value of the #define. If they are non-trivial, this might be a big win. If these constants are always numbers, hand-coding the rb_my_constants.c file may be the way to go (or you'd have to make parsing the .h file smarter (or give it hints?)). Besides, if new constants are added, you'd have to know they existed to use them in the Ruby code anyway. Perhaps Nathaniel can clear up the mystery for us by telling us what assumptions *he* is making and what solutions will work better for his situation. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com