LoginSignup
0
0

More than 3 years have passed since last update.

mapとeachの違い

Last updated at Posted at 2021-03-29

map

配列の入った変数.map {|変数名| 処理内容 }

food = ["fish", "beef", "banana","potato"]
food.map {|a| a.length}

#元の配列の変更していない
#戻り値は [4, 4, 6, 6]

map!(破壊的メソッド)

配列の入った変数.map! {|変数名| 処理内容 }

food = ["fish", "beef", "banana","potato"]
food.map! {|a| a.length}

#元の配列の変更している
#戻り値は [4, 4, 6, 6]

each(mapとの違い)

food = ["fish", "beef", "banana","potato"]
food.each {|a| a.length}

#戻り値がもとの配列を返す
#戻り値は ["fish", "beef", "banana","potato"]
0
0
1

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