# Clone array elements

**URL:** <https://www.ruby-forum.com/t/clone-array-elements/185049>\
**Category:** Ruby\
**Created:** [March 12, 2010, 3:32pm UTC](https://www.ruby-forum.com/t/clone-array-elements/185049 "2010-03-12T15:32:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![guilhermemm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@guilhermemm](https://www.ruby-forum.com/u/guilhermemm)\
**Post date:** [March 12, 2010, 3:32pm UTC](https://www.ruby-forum.com/t/clone-array-elements/185049/1 "2010-03-12T15:32:58Z")

</div>

How to create the repeat method:

[:one, “two”, 3].repeat(3)

Result:

[:one, :one, :one, “two”, “two”, “two”, 3, 3, 3]

Thanks.

---

<div class="post-metadata">

**Author:** ![guilhermemm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@guilhermemm](https://www.ruby-forum.com/u/guilhermemm)\
**Post date:** [March 12, 2010, 4:41pm UTC](https://www.ruby-forum.com/t/clone-array-elements/185049/2 "2010-03-12T16:41:38Z")

</div>

Guilherme M. wrote:

> How to create the repeat method:
> 
> [:one, “two”, 3].repeat(3)
> 
> Result:
> 
> [:one, :one, :one, “two”, “two”, “two”, 3, 3, 3]
> 
> Thanks.

I solve this using this method:

def repeat n  
new\_array = Array.new  
self.each { |x| n.times do new\_array.push x end }  
new\_array  
end

Thanks.

---

<div class="post-metadata">

**Author:** ![guilhermemm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@guilhermemm](https://www.ruby-forum.com/u/guilhermemm)\
**Post date:** [March 12, 2010, 4:44pm UTC](https://www.ruby-forum.com/t/clone-array-elements/185049/3 "2010-03-12T16:44:56Z")

</div>

Guilherme M. wrote:

> How to create the repeat method:
> 
> [:one, “two”, 3].repeat(3)
> 
> Result:
> 
> [:one, :one, :one, “two”, “two”, “two”, 3, 3, 3]
> 
> Thanks.

like this?

irb(main):018:0\> a=[:one,“two”,3]  
=\> [:one, “two”, 3]  
irb(main):019:0\> n=3; res=[]; a.each { |e| n.times { |i| res.push e } };  
res  
=\> [:one, :one, :one, “two”, “two”, “two”, 3, 3, 3]

---

<div class="post-metadata">

**Author:** ![guilhermemm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@guilhermemm](https://www.ruby-forum.com/u/guilhermemm)\
**Post date:** [March 13, 2010, 7:35am UTC](https://www.ruby-forum.com/t/clone-array-elements/185049/4 "2010-03-13T07:35:45Z")

</div>

On Fri, Mar 12, 2010 at 11:33 PM, Guilherme M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> How to create the repeat method:
> 
> [:one, “two”, 3].repeat(3)
> 
> Result:
> 
> [:one, :one, :one, “two”, “two”, “two”, 3, 3, 3]

Another way maybe,

class Array  
def repeat(num)  
Array.new(num,self).transpose.flatten  
end  
end

p [:one,“two”,3].repeat(3) #\> [:one, :one, :one, “two”, “two”,  
“two”, 3, 3, 3]

Harry

---

<div class="post-metadata">

**Author:** ![guilhermemm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@guilhermemm](https://www.ruby-forum.com/u/guilhermemm)\
**Post date:** [March 15, 2010, 10:18am UTC](https://www.ruby-forum.com/t/clone-array-elements/185049/5 "2010-03-15T10:18:55Z")

</div>

2010/3/13 Harry K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Another way maybe,
> 
> class Array  
> def repeat(num)  
> Array.new(num,self).transpose.flatten  
> end  
> end
> 
> p [:one,“two”,3].repeat(3) #\> [:one, :one, :one, “two”, “two”,  
> “two”, 3, 3, 3]

This is nice!

Since we didn’t have #inject in a long time:

module Enumerable  
def repeat(n)  
inject [] do |res, it|  
n.times { res \<\< it }  
res  
end  
end  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![guilhermemm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@guilhermemm](https://www.ruby-forum.com/u/guilhermemm)\
**Post date:** [March 13, 2010, 3:35am UTC](https://www.ruby-forum.com/t/clone-array-elements/185049/6 "2010-03-13T03:35:13Z")

</div>

Hi,

2010/3/12 Guilherme M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> How to create the repeat method:
> 
> [:one, “two”, 3].repeat(3)
> 
> Result:
> 
> [:one, :one, :one, “two”, “two”, “two”, 3, 3, 3]

You can do it like this:

irb(main):003:0\> [:one,“two”,3].map{|x|[x]\*3}.flatten(1)  
=\> [:one, :one, :one, “two”, “two”, “two”, 3, 3, 3]

Regards,

Park H.
