拆分数组中的数组

时间:2015-08-28 16:41:39

标签: arrays ruby string join

我想在数组中拆分数组,并将第二个元素的字符串混合。

[["c","a","t"], ["d","o","g"]]
...
# => "ao"

我该怎么办?我尝试了join方法并获得了第二个和第五个方法。但是当字长不同时:

[["b","i","r","d"], ["c","a","t"]]

我不知道该怎么做。你能给我一些建议吗?

3 个答案:

答案 0 :(得分:5)

> ary = [["c","a","t"], ["d","o","g"]]
=> [["c", "a", "t"], ["d", "o", "g"]]
> ary.map{|a| a[1]}.join
=> "ao"

答案 1 :(得分:4)

您可以尝试这样的事情:

> [["c","a","t"], ["d","o","g"]].collect{|e| e[1]}.join
=> "ao"

答案 2 :(得分:0)

希望这会有所帮助

a = [["b","i","r","d"], ["c","a","t"]]
result = a.collect{ |_,s,*__| s }.join
p result 
# => "ia"