Hi,

From: "Calum Shaw-Mackay" <calum.shaw-mackay / virgin.net>
>
> I have a fixnum which is essentially a bit flag
> i.e.
> a=1,b=2,c=4,d=8 and so on
> I use binary & to work out if a particular flag is set and output a given
> message, unfortunatley this leads to lots of puts x if(value & CONST_A) ==
> CONST_A
> I pass in the messages in an array but the code looks very C-ish - is there
> any construct in ruby that will allow me to get around this long winded code
> without having a large array of boolean values
> 
> I thought something along the line of {pseudo codey}
> 
> mesgs = {}
> ..... # Fill a with mesgs
> bitflags.each {|flag| puts mesgs[flag] if flag}

If you only need to print status for the flags contained in
the hash, you could reverse the above and iterate on the
hash keys...

%irb --simple-prompt
>> CONST_A = 1
=> 1
>> CONST_B = 2
=> 2
>> CONST_C = 4
=> 4
>> mesgs = { CONST_A => "able", CONST_B => "baker", CONST_C => "charlie" }
=> {1=>"able", 2=>"baker", 4=>"charlie"}
>> bitmask = 3
=> 3
>> mesgs.each_pair {|val,msg| puts msg if (bitmask & val) == val }
able
baker


Note that the above will not normally come out in sorted order,
because of the unordered elements aspect of the hash table...
You could sort the keys if that were preferable:

>> mesgs.keys.sort.each {|val| puts mesgs[val] if (bitmask & val) == val }
able
baker


Hope this helps,

Bill