Ren*_*ens 2 ruby arrays indexing
我有两个数组:
["mo", "tu", "we", "th", "fr", "sa", "su"] 和 [1, 5]
基于第二个数组的索引,从第一个数组创建新数组的最短,最干净的方法是什么?我想做这样的事情:
["mo", "tu", "we", "th", "fr", "sa", "su"][[1, 5]] (不可能这样)
这会产生["tu", "sa"].
怎么可以这样做?提前致谢!
使用Array#values_at尝试如下
a = ["mo", "tu", "we", "th", "fr", "sa", "su"]
b= [1, 5]
c = a.values_at(*b)
# => ["tu", "sa"]
Run Code Online (Sandbox Code Playgroud)