LoginSignup
87
65

More than 5 years have passed since last update.

Rubyの array.map(&:to_s) 記法を紐解く

Last updated at Posted at 2015-04-29

予備知識

  1. オブジェクトは&で修飾でき、 blockをprocに、 procをblockに 変換する。
  2. &はさらに、 procでないオブジェクトに対してto_procを呼んでからblockに変換する。
  3. Symbol#to_procは、引数を1つとり、そのsymbolをメソッドとして呼ぶprocを返す。

具体的には、:to_s.to_proc

Proc.new do |elem|
  elem.send(:to_s)
end

を返す。

結論

array = [1,2,3]
array.map(&:to_s)
# => ["1", "2", "3"]

出典
http://www.reddit.com/r/ruby/comments/2ipgx6/help_understanding_mapjoin/

87
65
3

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
87
65