# Noob: The ruby way to multiple Array by multiple?

**URL:** <https://www.ruby-forum.com/t/noob-the-ruby-way-to-multiple-array-by-multiple/56116>\
**Category:** Ruby\
**Created:** [March 28, 2006, 12:15pm UTC](https://www.ruby-forum.com/t/noob-the-ruby-way-to-multiple-array-by-multiple/56116 "2006-03-28T12:15:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e4f/32.png) [@eric99](https://www.ruby-forum.com/u/eric99)\
**Post date:** [March 28, 2006, 12:15pm UTC](https://www.ruby-forum.com/t/noob-the-ruby-way-to-multiple-array-by-multiple/56116/1 "2006-03-28T12:15:23Z")

</div>

I want to make a Array multipled by another Array, and the following  
semantic.  
For example:  
[1, 2, 3] \* [4, 5]  
then I will want [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]  
returned.

I have the following code snippet to implement this function:

class Array  
alias\_method :orig\_multiple, :\*  
def \*(arg)  
case arg  
when Array  
if arg.empty?  
self  
else  
inject([]) do |product, item|  
product.concat(arg.collect do |i|  
case item  
when Array  
item.clone \<\< i  
else  
[item, i]  
end  
end)  
end  
end  
else  
orig\_multiple(arg)  
end  
end  
end

It works but seems ugly.

Could anyone please to give me a clue to make it beauty and rubish!

Thx in advance.  
Eric

---

<div class="post-metadata">

**Author:** ![eric99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e4f/32.png) [@eric99](https://www.ruby-forum.com/u/eric99)\
**Post date:** [March 28, 2006, 12:30pm UTC](https://www.ruby-forum.com/t/noob-the-ruby-way-to-multiple-array-by-multiple/56116/2 "2006-03-28T12:30:50Z")

</div>

Eric L. wrote:

> ```
> case arg
> [item, i]
> 
> ```
> 
> It works but seems ugly.
> 
> Could anyone please to give me a clue to make it beauty and rubish!
> 
> Thx in advance.  
> Eric

res = []  
a1.each {|e1| a2.each {|e2| res \<\< [e1,e2]}}

Or, if you prefer inject

a1.inject([]) {|arr,e1| a2.each {|e2| arr \<\< [e1,e2]}; arr }

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![eric99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e4f/32.png) [@eric99](https://www.ruby-forum.com/u/eric99)\
**Post date:** [March 28, 2006, 12:55pm UTC](https://www.ruby-forum.com/t/noob-the-ruby-way-to-multiple-array-by-multiple/56116/3 "2006-03-28T12:55:29Z")

</div>

On Mar 28, 2006, at 2:08 AM, Eric L. wrote:

> I want to make a Array multipled by another Array, and the  
> following semantic.  
> For example:  
> [1, 2, 3] \* [4, 5]  
> then I will want [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]  
> returned.

a = [1, 2, 3]  
b = [4, 5]

(a \* b.size).zip(b \* a.size).sort

# =\> [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]

# or w/o sort:

a.zip(a).flatten.zip(b \* a.size)

# =\> [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]

# the latter only works as-is because b.size == 2 (a.zip(a).flatten

~= a \* 2).

# If b.size were 3, you’d add b.size-1 a’s to the first zip arg list:

b \<\< 6  
a.zip(a, a).flatten.zip(b \* a.size)

# =\> [[1, 4], [1, 5], [1, 6], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5],

[3, 6]]

# if order doesn’t actually matter, the first solution is the cleanest:

class Array  
def x(b)  
a = self  
(a \* b.size).zip(b \* a.size)  
end  
end

a.x b  
=\> [[1, 4], [2, 5], [3, 6], [1, 4], [2, 5], [3, 6], [1, 4], [2, 5],  
[3, 6]]

---

<div class="post-metadata">

**Author:** ![eric99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e4f/32.png) [@eric99](https://www.ruby-forum.com/u/eric99)\
**Post date:** [March 28, 2006, 2:32pm UTC](https://www.ruby-forum.com/t/noob-the-ruby-way-to-multiple-array-by-multiple/56116/4 "2006-03-28T14:32:32Z")

</div>

Ryan D. schrieb:

> ```
> a = self
> (a * b.size).zip(b * a.size)
> 
> ```
> 
> end  
> end
> 
> a.x b  
> =\> [[1, 4], [2, 5], [3, 6], [1, 4], [2, 5], [3, 6], [1, 4], [2, 5], [3,  
> 6]]

Ryan, this works only if a.size and b.size are relatively prime, as in  
your first example. The second example shows the error. If you use sort  
in a different place it should work, though:

(a \* b.size).sort.zip(b \* a.size)

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![eric99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e4f/32.png) [@eric99](https://www.ruby-forum.com/u/eric99)\
**Post date:** [March 29, 2006, 6:09am UTC](https://www.ruby-forum.com/t/noob-the-ruby-way-to-multiple-array-by-multiple/56116/5 "2006-03-29T06:09:17Z")

</div>

Ryan\> class Array  
Ryan\> def x(b)  
Ryan\> a = self  
Ryan\> (a \* b.size).zip(b \* a.size)  
Ryan\> end  
Ryan\> end

Ryan\> a.x b  
Ryan\> =\> [[1, 4], [2, 5], [3, 6], [1, 4], [2, 5], [3, 6], [1, 4], [2,  
5],  
Ryan\> [3, 6]]

Thanks for your hint, It seems so simple and pretty.
