I saw a line in a Rails app like this:
Order::PAYMENT_TYPES.map {|p| [t('.payment_type.'+p), p]}
PAYMENT_TYPE is a string array, and the letter t is used for i18n in Rails.
I’m not sure how the square brackets are used here. Apparently they are not for arrays or methods. And I will rewrite this to just {|p| t('.payment_type.'+p) }.
So what’s the Ruby grammar in this example?
Ruby returns the last statement from a method or a block. In this case, with the brackets, the block returns an array of two items, so calling that block in
map, ifPAYMENT_TYPEShad three items, would result in something like[ [a1, b1], [a2, b2], [a3, b3] ].