Ruby 1.9

tap

to_proc

Symbol

The & operator converts Proc objects into blocks and block into Proc objects. In this case, it tries to convert the symbol :+ into a block. The conversion uses Ruby's built-in coercion mechanism. That mechanism checks to see whether we have a Proc object. If not, it sends the #to_proc method to the argument to make a Proc. If the Symbol :+ has a #to_proc method, it will be called. In Ruby 1.9 it has a #to_proc method. That method returns a Proc that takes its first argument and sends the + method to it along with any other arguments that may be present.

So, &:+ really means { |x, y| x + y } .

plus = :+.to_proc

puts plus.call(1,2) # prints '3'

Symbol

to_proc

to_s = :to_s.to_proc

to_s.call(42) # results in the string "42"

to_proc

def to_proc

proc { |obj, *args| obj.send(self, *args) }

end

It creates a Proc which, when called on an object, sends that object the symbol itself. So, when names.map(&:upcase) starts to iterate over the strings in names, it'll call the block, passing in the first name and invoking its upcase method.

to_proc

Symbol

to_proc

Symbol

Symbol#to_proc

idiom

(1..100).map(&:to_s)

(1..100).map{|x| x.to_s }

Proc#curry

.

It's not difficult at all,

proc {|x, y, z| x + y + z }.curry

returns the proc object equivalent to

proc {|x| proc {|y| proc {|z| x + y + z } } }

[..] is the technique of transforming a function that takes multiple arguments into a function that takes a single argument (the other arguments having been specified by the curry).

x

x - 1)

plus_five = proc { |x,y,z| x + y + z }.curry.call(2).call(3)

plus_five[10] #=> 15

plus_five

[]

Proc#curry



