I find a lot of reference about removing duplicates in ruby but I cannot find how to create duplicate.
If I have an array like [1,2,3] how can I map it to a
[1,2,3]
@Ursus answer is the most clean, there are possible solutions:
a = [1, 2, 3] a.zip(a).flatten #=> [1, 1, 2, 2, 3, 3]
Or
a.inject([]) {|a, e| a << e << e} # a.inject([]) {|a, e| n.times {a << e}; a} => [1, 1, 2, 2, 3, 3]
[a, a].transpose.flatten # ([a] * n).transpose.flatten => [1, 1, 2, 2, 3, 3]