Ruby enums in C

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

On Dec 7, 2006, at 14:59 , Joe Van D. 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?

rb_iv_set(joe, “@mood”, rb_intern(“sad”));


Eric H. - [email protected] - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!

Joe Van D. 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

On 12/7/06, Jeremy W. [email protected] wrote:

Joe Van D. 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

constants in C - Google Search

Is there much difference between using a C constant for that, and
using a Ruby instance variable (like how Mr. Hodel did)?

On Dec 7, 2006, at 15:34 , Joe Van D. wrote:

An instantiated Joe object

constants in C - Google Search
+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 H. - [email protected] - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!