LoginSignup
0
0

More than 5 years have passed since last update.

each とmapの違い(自分メモ用)

Last updated at Posted at 2019-03-04

・eachはブロック内の処理はeach自身には反映されない。
・mapはブロックの戻り値がmap自体にも反映される。

m = [[6, 1, 8], [7, 5, 3], [2, 0, 0]]

これを真ん中の1,5,0だけ抽出したい時に

mapの時
 m.map{|a|a[1]} => [1, 5, 0]
eachの時
 m.each{|a|a[1]} => [[6, 1, 8], [7, 5, 3], [2, 0, 0]]

となり、eachの場合だと{}の処理後に反映されていない。
引数(m)と同じ扱いになる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0