0

我有一个多维数组:-

arr=  [[ ["paris", "P"], ["spain", "S"] ], [ ["russia", "R"], ["England", "E"] ]... 
  1. 我想选择这个数组的一个元素,并想要拆分它的元素,如 ["paris", "P"] 和 ["spain", "S"]
arr.each do |x|
   arr_new<< el1=x.first and el2=x.last
   end
  1. 给定此 arr2 的单个元素,我想选择第二个元素并推入 arr2:-
arr2=[]
arr2<<sec_el1=el1.last and sec_el2=el2.last.

什么是正确的红宝石方式?

4

1 回答 1

0

如果您希望达到的最终结果是抓取PS

 arr2=[]
 arr=  [["paris", "P"], ["spain", "S"] ], [ ["russia", "R"], ["England", "E"] ]
 arr.flatten.each_with_index{ |e,i| (arr2 << e) if i.odd?}
 puts arr2 #=> ["P", "S", "R", "E"]
于 2013-10-21T10:33:05.713 回答