Magic of Ruby &
Ruby has the syntax feature like &
with many use scenario. I combined some interesting posts I have found.
Let’s start from the basics. Ruby has the following syntax feature:
which means
It works because Ruby expands &:name
to &:name.to_proc
and adds method to_proc for the Symbol class
Consequences
The &
operator may take any proc object or the objects that can be converted to proc by to_proc method.
You can use the self’s methods
Parameters
You can supply arguments to &
syntax too. (ref [2])
As we saw above the proc
object could be taken from the method of an instance so for the simple arithmetic this works too.
Reference links
Compiled from the following posts