Symbol-to-proc in Ruby
Most of us use this without knowing how it works:
Yes it adds the numbers, but how does this work?
Actually, whenever Ruby finds an ¶meter
it expects the parameter
to be a Proc
object.
If not, Ruby tries to convert it into a proc by calling to_proc
method on it.
Turns out the Symbol
class has a handy to_proc
method which returns a proc which sends the method to receiving object. Something like this:
to_proc
returns a simple Proc
which sends the symbol(self) to the object yielded by the enumerator.
This code is for representation of how
Symbol#to_proc
works, actual code is written in C and is much more sophisticated.