[#4766] Wiki — "Glen Stampoultzis" <trinexus@...>

21 messages 2000/09/04
[#4768] RE: Wiki — "NAKAMURA, Hiroshi" <nahi@...> 2000/09/04

Hi, Glen,

[#4783] Re: Wiki — Masatoshi SEKI <m_seki@...> 2000/09/04

[#4785] Re: Wiki — "NAKAMURA, Hiroshi" <nakahiro@...> 2000/09/05

Howdy,

[#4883] Re-binding a block — Dave Thomas <Dave@...>

16 messages 2000/09/12

[#4930] Perl 6 rumblings -- RFC 225 (v1) Data: Superpositions — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/09/15

[#4936] Ruby Book Eng. translation editor's questions — Jon Babcock <jon@...>

20 messages 2000/09/16

[#5045] Proposal: Add constants to Math — Robert Feldt <feldt@...>

15 messages 2000/09/21

[#5077] Crazy idea? infix method calls — hal9000@...

This is a generalization of the "in" operator idea which I

17 messages 2000/09/22

[#5157] Compile Problem with 1.6.1 — Scott Billings <aerogems@...>

When I try to compile Ruby 1.6.1, I get the following error:

15 messages 2000/09/27

[ruby-talk:4824] RE: representing binary state in plain Ruby obje ct

From: Aleksi Niemel<aleksi.niemela@...>
Date: 2000-09-07 18:49:33 UTC
List: ruby-talk #4824
> I'd like an object to have binary states (each state is either true of
> false). If I were using plain C, I'd do
...
> is there some convenience and fast function?

Well, if you want bit-operations they are all there already. Just look
http://dev.rubycentral.com/ref/ref_c_fixnum.html#Bitoperations
or the same for Bignums. With bignums you're not hitting the limit of 31
avalable flags.

Just a reminder, you should write the if in Ruby like

  if flag & MY_CONSTANT_FLAG_BIT_REPRESENTATION != 0 
                                                ^^^^
and the usage of the constants would be about the same as in C.

The other way round is to use plain index as constant:

  if flag[MY_CONSTANT_FLAG_BIT_INDEX] != 0

What's the best way to name those indexes is then a matter of taste. Coming
from the Perl land might lead one to use hashes like
indexLookup["flag_foo"]=2, but this, of course, has a performance penalty. 

I guess in Ruby you could make, at least, a constant generator, so adding or
removing a constant wouldn't cause any manual changes to the code like in C:

> typedef enum
> {
>      FLAG_FOO = 1 << 1,
>      FLAG_BAR = 1 << 2,

removing FLAG_FOO so that there's always room at the end of the integer:

  typedef enum
  {
       FLAG_BAR = 1 << 1,

The interface for the enum creator could be something like attr_* has:

  enum :flag_foo, :flag_bar

But I think I've seen something related to enums somewhere, just can't
recall...or I've imagined...in either case I'm getting too old :).

	- Aleksi

In This Thread

Prev Next