ð Instant summary
They are equal.
# ary = [:jack, :justin, :jojo]
ary.map(&:to_s) === ary.map{|obj| obj.to_s}
# => true
ary.map(&:to_s)
# => ["jack", "justin", "jojo"]
ary.map{|obj| obj.to_s}
# => ["jack", "justin", "jojo"]
ðŠ Understanding
1. The & pass Proc object as a block.
The & in arg, ruby pass the Proc object as a block.
ð map(&b) (Japanese)
â
2. automated to_proc
You may have a question that the to_s
isn't a Proc object. That's right ð and, ruby automatically calls Symbol#to_proc
before blocknized by the &.
It means that ..,
ary.map(&:to_s) === ary.map(&:to_s.to_proc)
# => true
â
3. Symbol#to_proc method
Returns a Proc object which respond to the given method by sym.
ary.map(&:to_s.to_proc) === ary.map(&Proc.new{|obj| obj.to_s })
# => true
ââ
Sumarry
They are same.
ary.map(&:to_s)
ary.map(&:to_s.to_proc)
ary.map(&Proc.new{|obj| obj.to_s })
ary.map{|obj| obj.to_s}
Top comments (1)
Oh.., Thank you! I will fix my explain!