From: Eric Hodel Date: 2006-12-08T09:39:20+09:00 Subject: Re: Ruby enums in C On Dec 7, 2006, at 15:34 , Joe Van Dyk wrote: > On 12/7/06, Jeremy Woertink wrote: >> Joe Van Dyk wrote: >> > Each instance of Joe has a MOOD which can be HAPPY, SAD, or >> NEUTRAL. >> > >> > The Joe class is written in C. In C, what's the appropriate way >> for >> > setting MOOD to one of the appropriate values? >> > >> > Thanks, >> > An instantiated Joe object >> >> http://www.google.com/search?hl=en&q=constants+in+C&btnG=Google >> +Search > > Is there much difference between using a C constant for that, and > using a Ruby instance variable (like how Mr. Hodel did)? A C constant takes more work to expose to Ruby. You have to map that opaque integer into something sensible, and then you have uglier code. #define JOE_SAD -1 #define JOE_NEUTRAL 0 #define JOE_HAPPY 1 /* ... */ rb_define_const(cJoe, "HAPPY", INT2FIX(JOE_HAPPY)); if Joe.state == Joe::HAPPY then ... end A Symbol in an instance variable gives you zero extra work, allows for easier debugging on the Ruby side and gives you prettier code. if Joe.state == :happy then ... end -- Eric Hodel - drbrain@segment7.net - http://blog.segment7.net I LIT YOUR GEM ON FIRE!