使用array.each_with_index
返回了一个数组,该数组包含我设置为等于的几个元素的索引indexes
。我只想知道如何在诸如的每个索引处找到元素array[indexes] #=> element 1, element 2, etc
。我没有运气就尝试了前面的示例,并且array[indexes.each {|x| x}]
。
不知道为什么这么难,但是我是编码的新手,在其他地方找不到答案。
那就是Array#values_at的作用:
indices = [0,2]
p ["a", "b", "c", "a"].values_at(*indices) # => ["a", "c"]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句