Ylan Segal

Ruby Implicit `to_proc`

Ruby’s blocks are one of the language features I like the most. The make iterating on collections extremely easy.

1
2
[1, 2, 3].map { |n| n.to_s }
=> ["1", "2", "3"]

You can shorten the (already short!) syntax above, like so:

1
2
[1, 2, 3].map &:to_s
=> ["1", "2", "3"]

The above is implicitly calling to_proc on the symbol. This es extremely handy, when you are calling the same method on each object. However, it can also be useful to call a method with each object as an argument:

1
2
3
4
5
6
7
def fancy_formatvalue)
 "==::[[#{value}]]::=="
end

['1', '3', 'a'].map &method(:fancy_format)

=> ["==::[[1]]::==", "==::[[3]]::==", "==::[[a]]::=="]

In addition, note that the number of arguments yielded to the method, depends on what the original implementation is. For hashes, this is especially useful:

1
2
3
4
5
6
7
8
9
10
11
def fancy_format(key, value)
  puts "==::[[#{key} - #{value}]]::=="
end

ball = { color: 'red', size: 'large', type: 'bouncy' }


>> ball.each &method(:fancy_format)
==::[[color - red]]::==
==::[[size - large]]::==
==::[[type - bouncy]]::==