Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
次のような配列があるとしましょう。
[1,2,3,4,5,6,7]
この配列の最初の数を除くすべての数を 2 で乗算して、新しい配列が次のようになるようにするにはどうすればよいですか
[1,4,3,8,5,12,7]
あなたは使用することができmapますwith_index:
map
with_index
[1,2,3,4,5,6,7].map.with_index{|v,i| i % 2 == 0 ? v : v * 2 } # => [1, 4, 3, 8, 5, 12, 7]
[1,2,3,4,5,6,7].each_slice(2).flat_map{|k, l| [k, *(l * 2 if l)]} # => [1, 4, 3, 8, 5, 12, 7]