J'ai un tableau comme celui-ci:Comment diviser un tableau de niveau à de nombreux tableaux dans Ruby 1.9.2
[234, 235, 343, 445]
Je veux convertir pour ressembler à ce
[[234], [235], [343], [445]]
y at-il la bibliothèque centrale f onction dans ruby 1.9.2 pourrait m'aider à le faire rapidement? et si non, y a-t-il un moyen rapide?
J'ai fait un petit essais
def test1
array = []
10000000.times do
array << rand(1000000)
end
time = Time.now
array.permutation(1).to_a
puts "test1 (permutation) ---> Time = #{Time.now - time}"
end
def test2
array = []
10000000.times do
array << rand(1000000)
end
time = Time.now
array.zip()
puts "test2 (zip)---> Time = #{Time.now - time}"
end
def test3
array = []
10000000.times do
array << rand(1000000)
end
time = Time.now
array.map { |a| [a] }
puts "test3 (map) ---> Time = #{Time.now - time}"
end
test1 #test1 (permutation) ---> Time = 2.235128
test2 #test2 (zip) ---> Time = 1.537088
test3 #test3 (map) ---> Time = 2.230127
Merci beaucoup à tous pour vos réponses, "zib" est le moyen le plus rapide ici – Amer