Symbol to Proc
TL;DR:
- Symbol to Proc is Awesome!
- You can use it as a shorthand
array.each(&:method_name)
Lets say we have array of names and we want to transform the names to upcase. One way to do this is to use the map method.
arr = %w(toni dare johnny tomaz)
arr = arr.map do |name|
name.upcase
end
p arr
# >> ["TONI", "DARE", "JOHNNY", "TOMAZ"]
You could shorten this by using symbol to proc. Let just look at the code and than see what is happening under the hood.
arr = %w(toni dare johnny tomaz)
arr = arr.map(&:upcase)
p arr
# >> ["TONI", "DARE", "JOHNNY", "TOMAZ"]
:upcase
is a symbol and when we ad &
infront of the symbol the .to_proc
method is called on the symbol.
Knowing this we can play around and monkey patch the Symbol class.
class Symbol
def to_proc
Proc.new { "Joe" }
end
end
arr = %w(toni dare johnny tomaz)
arr = arr.map(&:upcase)
p arr
# >> ["Joe", "Joe", "Joe", "Joe"]
If we wanted to reimplement rubys default behaviour we could write the code something like this
class Symbol
def to_proc
# self is :upcase
# we are sending message :upcase to a string object
# this is the same as "joe".upcase
-> (name) { name.send(self) }
end
end
arr = %w(toni dare johnny tomaz)
arr = arr.map(&:upcase)
p arr
# >> ["TONI", "DARE", "JOHNNY", "TOMAZ"]