From: Edwin Fine Date: 2006-11-22T02:34:01+09:00 Subject: Re: array question Li Chen wrote: > Thank you all for your invalulabe inputs. > > Based on what I understand and my personal preference I choose the > following scripts. > > > Li > > #### > array=[1,2,3,4,5] > > array_new=Array.new > array.each {|e| array_new<<[e,e]} > > array_new.flatten![1..-2].each{|e| print "#{e}\t" } > > puts > > ##output >>ruby sur3.rb > 1 2 2 3 3 4 4 5 >>Exit code: 0 This is the best I could come up with. It works with any kind of element including embedded arrays. array_new = (temp = array[1..-2]).zip(temp).inject([]){|n, e| n.concat(e) } irb(main):148:0> array=[1,[2,3],4,5,{6,7},"8"] => [1, [2, 3], 4, 5, {6=>7}, "8"] irb(main):149:0> (temp = array[1..-2]) => [[2, 3], 4, 5, {6=>7}] irb(main):150:0> (temp = array[1..-2]).zip(temp) => [[[2, 3], [2, 3]], [4, 4], [5, 5], [{6=>7}, {6=>7}]] irb(main):151:0> (temp = array[1..-2]).zip(temp).inject([]) {|new, elem| new.concat(elem) } => [[2, 3], [2, 3], 4, 4, 5, 5, {6=>7}, {6=>7}] -- Posted via http://www.ruby-forum.com/.