'Lo all
I’m impressed with both Ruby and JRuby - I mainly program in Java so ruby’s
OOP is fairly natural
I’ve got a question regarding some code I’ve ported…
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}
Would this be going along the right lines?
Thanks in advance
Calum
“Calum Shaw-Mackay” calum.shaw-mackay@virgin.net wrote in message
news:BEEOLJOGDFLPOHNICOIEIEEFCAAA.calum.shaw-mackay@virgin.net…
mesgs = {}
… # Fill a with mesgs
bitflags.each {|flag| puts mesgs[flag] if flag}
Would this be going along the right lines?
I haven’t worked with binary operators in Ruby, but you seem to be on the
right track. Certain you could have an integer, pick the lower bit, divide
be 2. Repeat until integer is zero. For each bit you pick you call yield.
For details on how to implement you own each function, see the PickAxe
book - it has good examples. Sorry for not banging up the code, I just
wanted to say that it would certainly be possible.
There might be some bitvector packages around for Ruby I seem to recall
Robert Feldt did something along those lines.
Indeed doing a Google reveals
http://www.ce.chalmers.se/~feldt/ruby/extensions/bitvector/
Mikkel
In article BEEOLJOGDFLPOHNICOIEIEEFCAAA.calum.shaw-mackay@virgin.net,
'Lo all
I’m impressed with both Ruby and JRuby - I mainly program in Java so ruby’s
OOP is fairly natural
Actually, if you’re used to Java, Ruby should seem supernatural
I’ve got a question regarding some code I’ve ported…
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}
Would this be going along the right lines?
Seems like a reasonable way to do it. I’m not quite understanding what
you’re putting in the bitflags array, but I figure that it’s an array of
true/false/nil values (right?)
Phil
···
Calum Shaw-Mackay calum.shaw-mackay@virgin.net wrote:
Hi,
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
···
From: “Calum Shaw-Mackay” calum.shaw-mackay@virgin.net
Fixnum already has bit references built-in as the operator:
a = 35
p a[0] #>> 1 (bit 0)
p a[1] #>> 1 (bit 1)
p a[2] #>> 0 (bit 2)
… etc
However, ‘0’ and ‘1’ are both treated as true. You could always override it
to return ‘false’ and ‘true’ instead.
Or maybe you just want a wrapper method for your existing constants:
class Fixnum
def bit_set?(x)
(self & x) != 0
end
end
p 35.bit_set?(1) # >>true
p 35.bit_set?(2) # >>true
p 35.bit_set?(4) # >>false
I notice there is a ‘bitvector’ class in RAA, haven’t looked at it though.
Regards,
Brian.
···
On Wed, Apr 02, 2003 at 07:03:47AM +0900, Calum Shaw-Mackay wrote:
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