Today I Learned

Always something.

Using Ruby Proc Shorthand With Arguments

When following the Ruby Style Guide on single operation blocks, there are times where that operation is a method that needs to take an argument. To still use the Proc shorthand in these cases, the Object#method method allows passing each element of the enumerable to the method.

LETTERS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r']

def number_to_letter(n)
  LETTERS[n]
end

# Lame
[17, 0, 12, 8].map { |s| number_to_letter(s) }

# Cool
[17, 0, 12, 8].map(&method(:number_to_letter))