I want to create a set of values in Ruby which I can store and retrieve in a MySQL databse under Rails.
In Delphi I would use:
//Create an enumeration with four possible values
type TColour = (clRed, clBue, clBlack, clWhite);
//Create a set which can hold values from the above enumeration
TColours = set of TColours;
//Create a variable to hold the set
var MyColours = TColours;
begin
//Set the variable to hold two values from the enumeration
MyColours = [clRed, clBlack];
//MyColours now contains clRed and clBlack, but not clBlue or clWhite
//Use a typecast to convert MyColours to an integer and store it in the database
StoreInDatabase(Integer(MyColours));
//Typecast the Integer back to a set to retrieve it from the database
MyColours := TColours(RetrieveFromDatabase);
end;
I can then use a typecast to convert to/from an integer.
How would I achieve the same in Ruby/Rails?
Just to clarify, suppose I had a form with check boxes for 'Red', 'Blue', 'Black', 'White'. The user can choose none, one, or more than one value. How to I store and retrieve that set of values?
BTW, another way of doing this in delphi is with bitwise maths:
const
Red = 1;
Blue = 2;
Black = 4;
White = 8;
var MyColours: Integer;
begin
MyColours := Red+Black; //(or MyColours = Red or Black)
which can be stored and retrieved as an integer