Using Integers to Store Bits of Information

Hi,

I am a very new to ruby on rails so please forgive me if someone has
asked this before.

I need to make a form with many checkboxes. Sometime ago I was using
Coldfusion and I chanced upon this great article on how to store flags
using binary numbers.
http://cfdj.sys-con.com/read/41674.htm

Just wondering if there is something similar in RubyOnRails that I can
learn from and use. That rails have done it easier.

Please note that I am very new to RubyOnRails.

P.V.Anthony

Please note that I am very new to RubyOnRails.

I don’t know of anything that will do this automatically, but you can
certainly do it. I do it right now manually just by shifting bits
around
with <<. Works fine.

-philip

Philip H. wrote:

Please note that I am very new to RubyOnRails.

I don’t know of anything that will do this automatically, but you can
certainly do it. I do it right now manually just by shifting bits
around
with <<. Works fine.

-philip

Fixnums support some bit-wise opperations
You can also store binary values explicitly:
a = 0b01

a[n] returns the nth bit, where a[0] is the least significant bit

See http://ruby-doc.org/core/classes/Fixnum.html for details

Cheers

Use composition.

Make a class that represents your flags as boolean attributes,
then have it map those attributes to integers in any convenient
way.

Do yourself a favor and DON’T expose your flagging implementation.

On Friday, July 07, 2006, at 11:23 AM, Tom M. wrote:

Hi,

can learn from and use. That rails have done it easier.
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

I think there’s a ‘acts_as_bitfield’ plugin somewhere.

_Kevin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs