# Listing variations of product attributes

**URL:** <https://www.ruby-forum.com/t/listing-variations-of-product-attributes/130763>\
**Category:** Rails\
**Created:** [March 3, 2008, 4:15am UTC](https://www.ruby-forum.com/t/listing-variations-of-product-attributes/130763 "2008-03-03T04:15:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mroths](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mroths](https://www.ruby-forum.com/u/mroths)\
**Post date:** [March 3, 2008, 4:15am UTC](https://www.ruby-forum.com/t/listing-variations-of-product-attributes/130763/1 "2008-03-03T04:15:03Z")

</div>

The situation is the following:

In my app every product has attributes. for example a shirt has color,  
neck size and sleeve size.

A jacket has color and jacket size.

Those attributes have values.

Colors: black, white, green, red etc  
Neck size: 14, 15, 16, 16 etc  
sleeve size: 20,21,22,23,24 etc  
jacket size: S, M, L, XL

what I want to achieve is dynamically list all the available combination  
(variation) of attributes for a specific type of product.

for example:

a shirt can have a combination of 4 colors, 5 neck sizes and 7 sleeve  
sizes = 140

so I want my app to list all the available variations for shirts like

black, 14, 20  
black, 14, 21  
black, 14, 22  
…  
and so on (all 140 variations in this case)

so first I want to loop through sleeve size attributes, and then neck  
size and finally colors.

what’s the best solution to achieve that? it has to be dynamic, so if  
the product is a jacket it has to know that it has 2 attributes (color  
and size) so it will loop through those like this:

black, S  
black, M  
black, L  
black, XL  
white, S  
white, M  
…  
and so on.

has anyone encountered that kind of problem and found a good solution?

Thanks for everyone.

---

<div class="post-metadata">

**Author:** ![mroths](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mroths](https://www.ruby-forum.com/u/mroths)\
**Post date:** [March 3, 2008, 5:02am UTC](https://www.ruby-forum.com/t/listing-variations-of-product-attributes/130763/2 "2008-03-03T05:02:12Z")

</div>

Mark Ro wrote:

> The situation is the following:
> 
> In my app every product has attributes. for example a shirt has color,  
> neck size and sleeve size.
> 
> A jacket has color and jacket size.
> 
> Those attributes have values.
> 
> Colors: black, white, green, red etc  
> Neck size: 14, 15, 16, 16 etc  
> sleeve size: 20,21,22,23,24 etc  
> jacket size: S, M, L, XL

irb(main):001:0\> class Array  
irb(main):002:1\> def cross(other)  
irb(main):003:2\> inject([]) {|a,b| other.map {|c| a \<\< [b,c].flatten};a}  
irb(main):004:2\> end  
irb(main):005:1\> end  
=\> nil  
irb(main):006:0\> [:black, :white].cross [14,15].cross %w{S M L}  
(irb):6: warning: parenthesize argument(s) for future version  
=\> [[:black, 14, “S”], [:black, 14, “M”], [:black, 14, “L”], [:black,  
15, “S”], [:black, 15, “M”], [:black, 15, “L”], [:white, 14, “S”],  
[:white, 14, “M”], [:white, 14, “L”], [:white, 15, “S”], [:white, 15,  
“M”], [:white, 15, “L”]]  
irb(main):007:0\>

fix that parenthesize thingy and you should be good to go…

hth

ilan

---

<div class="post-metadata">

**Author:** ![mroths](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mroths](https://www.ruby-forum.com/u/mroths)\
**Post date:** [March 3, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/listing-variations-of-product-attributes/130763/3 "2008-03-03T18:19:13Z")

</div>

Brilliant, thanks.

Let’s take a real examples:

colors = [“black”,“white”,“red”,“blue”]  
neck = [14,15,16,17,18]  
sleeve = [30,31,32,33,34]

how can I change the above function (cross), so it returns a hash that  
includes the name of the attribute as well like:

{1=\>[“color” =\>“black”, “neck” =\> 14, “sleeve” =\> 30],  
2=\>

---

<div class="post-metadata">

**Author:** ![mroths](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mroths](https://www.ruby-forum.com/u/mroths)\
**Post date:** [March 3, 2008, 6:22pm UTC](https://www.ruby-forum.com/t/listing-variations-of-product-attributes/130763/4 "2008-03-03T18:22:19Z")

</div>

sorry I submitted the previous post before I finished it.

so I want a returned hash something like this:

{1=\>[“color” =\>“black”, “neck” =\> 14, “sleeve” =\> 30],  
2=\>[“color” =\>“black”, “neck” =\> 14, “sleeve” =\> 31],  
3=\>[“color” =\>“black”, “neck” =\> 14, “sleeve” =\> 32],

…

100=\>[“color” =\>“blue”, “neck” =\> 18, “sleeve” =\> 34]}

thanks
