I recently found that the Array class of Ruby has a zip method, which works much like Haskell’s zip function, except that it allows multiple arguments.

For example:



$ irb --simple-prompt

>> [1,2,3].zip [4,5,6], [7,8,9]

=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]



The night after I did this little experiment I suddenly realized: this is a transpose function! I quickly got out of bed to document my little discovery:



def transpose(matrix)

matrix[0].zip *matrix[1..-1]

end



It’s the little things that make me like this language so much.

Update: I forgot the asterisk before matrix[1..-1] in the above method.