From: Guilherme Mello on
How to create the repeat method:

[:one, "two", 3].repeat(3)

Result:

[:one, :one, :one, "two", "two", "two", 3, 3, 3]

Thanks.
--
Posted via http://www.ruby-forum.com/.

From: Guilherme Mello on
Guilherme Mello 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.
--
Posted via http://www.ruby-forum.com/.

From: Riccardo Cecolin on
Guilherme Mello 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]


From: Heesob Park on
Hi,

2010/3/12 Guilherme Mello <javaplayer(a)gmail.com>:
> 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 Heesob

From: Harry Kakueki on
On Fri, Mar 12, 2010 at 11:33 PM, Guilherme Mello <javaplayer(a)gmail.com> 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