From: Guilherme Mello on 12 Mar 2010 09:33 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 12 Mar 2010 10:41 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 12 Mar 2010 10:41 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 12 Mar 2010 21:33 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 13 Mar 2010 01:34
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 |