本文介绍了将子阵列整齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前工作的一个大阵的名称上:
large_array =鲍勃,乔尔,约翰,史密斯,凯文,将要,斯坦利,乔治]#,然后让上
我将它分成子阵列,像这样:
large_array.each_slice(2).to_a#=> [鲍勃,乔尔],[约翰,史密斯],[凯文,将要],[斯坦利,乔治]]
我的问题是如何使子阵列在彼此的顶部整齐地排出现这样的:
[鲍勃,乔尔]
[约翰,史密斯]
[凯文,将要]
[斯坦利,乔治]
解决方案
large_array.each_slice(2){| A |把a.inspect}
#[鲍勃,乔尔]
#[约翰,史密斯]
#[凯文,将要]
#[斯坦利,乔治]
#=>零
I am currently working on a large array of names:
large_array = ["Bob","Joel","John","Smith","Kevin","Will","Stanley","George"] #and so on
I split it into sub arrays like so:
large_array.each_slice(2).to_a #=> [["Bob", "Joel"],["John,"Smith"],["Kevin", "Will"],["Stanley","George"]]
My question is how do I make the sub arrays appear neatly on top of each other in rows like this:
["Bob", "Joel"]
["John,"Smith"]
["Kevin","Will"]
["Stanley","George"]
解决方案
large_array.each_slice(2) {|a| puts a.inspect}
# ["Bob", "Joel"]
# ["John", "Smith"]
# ["Kevin", "Will"]
# ["Stanley", "George"]
# => nil
这篇关于将子阵列整齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!