Ruby で
[1, 6, 4, 3, 6] + [5, 4, 3, 2, 1] = [6, 10, 7, 5, 7]
をやる一つの方法。
ary1 = [1, 6, 4, 3, 6]
ary2 = [5, 4, 3, 2, 1]
ary1.zip(ary2).map {|a, b| a + b} # => [6, 10, 7, 5, 7]
「n個の配列の各要素をそれぞれ〇〇した配列を作成」には、ほとんどの場合 .zip.map
が使える。
Go to list of users who liked
More than 5 years have passed since last update.
Ruby で
[1, 6, 4, 3, 6] + [5, 4, 3, 2, 1] = [6, 10, 7, 5, 7]
をやる一つの方法。
ary1 = [1, 6, 4, 3, 6]
ary2 = [5, 4, 3, 2, 1]
ary1.zip(ary2).map {|a, b| a + b} # => [6, 10, 7, 5, 7]
「n個の配列の各要素をそれぞれ〇〇した配列を作成」には、ほとんどの場合 .zip.map
が使える。
Register as a new user and use Qiita more conveniently
Go to list of users who liked