LoginSignup
2
2

More than 5 years have passed since last update.

mapとかに関数を渡す

Last updated at Posted at 2012-11-29

たとえば数字のリストから文字列のリストを作る時

list = [1, 2, 3, 4, 5]
list.map{|i| i.to_s} #=> ["1", "2", "3", "4", "5"]

とか指定するけど、Ruby1.9以降なら

list = [1, 2, 3, 4, 5]
list.map(&:to_s)

とすると全要素にシンボルで指定した関数を実行できる


Symbol#to_proc を使っているとのこと

詳しい解説はこのページがみやすい
Rubyで .map(&:first) と .map{|e| e.first } が同じになる理由は?

2
2
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
2
2