# Sets in SQL/Ruby

**URL:** <https://www.ruby-forum.com/t/sets-in-sql-ruby/71914>\
**Category:** Ruby\
**Created:** [October 12, 2006, 10:50am UTC](https://www.ruby-forum.com/t/sets-in-sql-ruby/71914 "2006-10-12T10:50:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![surf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@surf](https://www.ruby-forum.com/u/surf)\
**Post date:** [October 12, 2006, 10:50am UTC](https://www.ruby-forum.com/t/sets-in-sql-ruby/71914/1 "2006-10-12T10:50:53Z")

</div>

I have been working on building a common library that represents  
fields as weighted sets.  
I am more familiar with Rails know than I was when I started this. I  
decided to post this because I was concerned I may need to show my code  
in a job interview.

Below is a code sample of how the sets are declared. The example is of  
3 different sets.  
I map each set element as a name of the element to a symbol of length  
one. Then the set is  
stored in the database field as a char and a digit (since it’s  
weighted). so  
“w3u4a0” is the set [windows:3, linux:4, aix:0]. The obvious weakness  
is that I can only really have around 26 elements in the set type, but  
that seemed ok for what I was doing. If I list my SQL tables from SQL,  
the sets are partially descernable more so than just numbers are.  
Possibly some bit map could have been used, but it might have been  
harder to read from a select. I also may have doubly weighted sets as a  
type such as represented by “w32l44”. It all works fine as long as each  
weight is one digit. Anyway, I just had a java code review at a job  
interview and realized the extent to which people can critique your  
code so that inspired me to post this here in case I had to use my code  
for a job interview etc.

{:platforms =\>  
{:windows =\> :w,  
:linux =\> :l,  
:unix =\> :u,  
:aix =\> :a,  
:solaris =\> :s  
}  
},  
{:frameworks =\>  
{:rails =\> :r,  
:j2ee =\> :m,  
:dot\_net =\> :d  
}  
},  
{:work =\>  
{:telecommute =\> :t,  
:contract =\> :c,  
:permanent =\> :s,  
:part\_time =\> 😛  
}

---

<div class="post-metadata">

**Author:** ![surf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@surf](https://www.ruby-forum.com/u/surf)\
**Post date:** [October 12, 2006, 11:06am UTC](https://www.ruby-forum.com/t/sets-in-sql-ruby/71914/2 "2006-10-12T11:06:57Z")

</div>

surf wrote:

> I have been working on building a common library that represents  
> fields as weighted sets.  
> Below is a code sample of how the sets are declared. The example is of  
> 3 different sets.  
> I map each set element as a name of the element to a symbol of length  
> one. Then the set is  
> stored in the database field as a char and a digit (since it’s  
> weighted).

I guess the main point here is the database representation. The  
solution you have given may be fine given that the data domain can be  
limited to those constraints (single char and single digit). You could  
increase this a bit by coding the weight and/or element to another base  
(i.e. using the full 256 byte values as representation). But then you  
loose readability if that is important. You could use a variable length  
encoding with a separator character, like so:

element,12,another,4,bigone,12345

Or you could go the whole way and store a YAML representation of the  
data set in the database. That’d give you maximum flexibility, but  
depending on how the data is to be used may or may not be appropriate.
