# Creating an array containing sums of combinations of ranges

**URL:** https://rubytalk.org/t/creating-an-array-containing-sums-of-combinations-of-ranges/65269
**Category:** ruby-talk
**Created:** [14 April 2012 23:54 UTC](https://rubytalk.org/t/creating-an-array-containing-sums-of-combinations-of-ranges/65269 "2012-04-14T23:54:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)
#### Post date: [14 April 2012 23:54 UTC](https://rubytalk.org/t/creating-an-array-containing-sums-of-combinations-of-ranges/65269/1 "2012-04-14T23:54:41Z")

</div>

I've got a puzzle I haven't been able to solve. I have two user-provided  
arrays that look something like this:

[1..2, 2..3]

and

[2, 3]

I need to produce an array containing every possible sum of the ranges  
multiplied by the corresponding member of the second array. i.e. if the  
first array is [n, m] and the second array is [x, y], I need an array  
containing every possible (nx + my).

Normally I'd do a couple of nested loops, but I need the arrays can be  
of arbitrary lengths.

How should I go about this?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Ralph\_Shnelvar](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@Ralph\_Shnelvar](https://rubytalk.org/u/Ralph_Shnelvar)
#### Post date: [15 April 2012 01:54 UTC](https://rubytalk.org/t/creating-an-array-containing-sums-of-combinations-of-ranges/65269/2 "2012-04-15T01:54:57Z")

</div>

Jeff,

Saturday, April 14, 2012, 5:54:41 PM, you wrote:

> I've got a puzzle I haven't been able to solve. I have two user-provided  
> arrays that look something like this:

> [1..2, 2..3]

> and

> [2, 3]

> I need to produce an array containing every possible sum of the ranges  
> multiplied by the corresponding member of the second array. i.e. if the  
> first array is [n, m] and the second array is [x, y], I need an array  
> containing every possible (nx + my).

> Normally I'd do a couple of nested loops, but I need the arrays can be  
> of arbitrary lengths.

> How should I go about this?

Could you give some more examples. Your English is a bit ambiguous.

Have you thought about using something like Cucumber to specify the problem?

Ralph Shnelvar

---

<div class="post-metadata">

### Author: ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)
#### Post date: [15 April 2012 02:49 UTC](https://rubytalk.org/t/creating-an-array-containing-sums-of-combinations-of-ranges/65269/3 "2012-04-15T02:49:16Z")

</div>

hi Jeff,

&nbsp;&nbsp;have you checked out Array#product ? seems like that might do the  
trick for you...

a1 = ['n', 'm']  
a2 = ['x', 'y']

p a1.product(a2)

=\> [["n", "x"], ["n", "y"], ["m", "x"], ["m", "y"]]

&nbsp;&nbsp;if your first array is composed of ranges, those can easily be  
converted into arrays...

&nbsp;&nbsp;hth-

&nbsp;&nbsp;- j

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)
#### Post date: [15 April 2012 03:47 UTC](https://rubytalk.org/t/creating-an-array-containing-sums-of-combinations-of-ranges/65269/4 "2012-04-15T03:47:55Z")

</div>

Jake,  
That's exactly what I was looking for. Thank you!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
