[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby enums in C

Joe Van Dyk

12/7/2006 10:59:00 PM

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

4 Answers

Eric Hodel

12/7/2006 11:04:00 PM

0

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

rb_iv_set(joe, "@mood", rb_intern("sad"));

--
Eric Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!


Jeremy Woertink

12/7/2006 11:06:00 PM

0

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=Goo...

--
Posted via http://www.ruby-....

Joe Van Dyk

12/7/2006 11:34:00 PM

0

On 12/7/06, Jeremy Woertink <its_conebred_foo@hotmail.com> 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=Goo...

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

Eric Hodel

12/8/2006 12:39:00 AM

0

On Dec 7, 2006, at 15:34 , Joe Van Dyk wrote:
> On 12/7/06, Jeremy Woertink <its_conebred_foo@hotmail.com> 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&b...
>> +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.se...

I LIT YOUR GEM ON FIRE!