views:

469

answers:

3

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

+1  A: 

Based on comments and revised question, sounds like you want try out the Enum Column plugin for Rails. Has a helper, enum_radio(), that would probably be useful in your form.

rnicholson
I think he means to use Enum.
Swanand
Yes, Enum, whatever it is sounds like it's heading in the right direction. But what I need is a field that can store zero or more values from an enumeration, like a line of check boxes where none, one, or more than one are checked.
Mike Sutton
Good point. Although, Ruby really doesn't have an enum type (could hack it I guess with a module and constants). I guess I was just keying off the "I want to create a set of values in Ruby which I can store and retrieve in a MySQL databse under Rails."
rnicholson
Updated with link to plugin
rnicholson
Thanks, but I need to be able to store a number of values from the enumeration to the database - I'll add comments to my code above.
Mike Sutton
+3  A: 

Here is a simple implementation for the bitwise solution:

module Colors
  Red = 1
  Blue = 2
  Black = 4
  White = 8
  ColorsSet = [Red,Blue,Black,White]

  # Mixing valid colors
  def self.mix(*colors) 
    colors.inject{|sum,color| ColorsSet.include?(color) ? color | sum : sum }
  end

  # Get an array of elements forming a mix
  def self.elements(mix)
    ColorsSet.select{|color| color & mix > 0}
  end
end

mix = Colors::mix(Colors::Red,Colors::Blue)

puts mix #=> 3

puts Colors::elements(mix) 
#=> 1
#   2
khelll
Thanks. I was hoping Ruby would have a neat, ready rolled solution, but that looms pretty simple and effective.
Mike Sutton
A: 

Maybe you find this easier

Colors={:red=>1, :blue=>2, :black=>4, :white=>8}
mix = Colors[:blue] + Colors[:black]
Colors.select{|key,value| value & mix > 0}
=> [[:blue, 2], [:black, 4]]
Jonas Elfström