Last Updated: February 25, 2016
·
1.325K
· hauleth

Use `#zip` and `#map` to join 2 arrays

Some time ago someone ask how to "push to array in more elegant way". His code (of course ugly as hell and absolutely non-rubish)

valrow = [ 0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875]
lblrow = [48,  8539,  188,  8540, 189,  8541,  190,  8542]
opts = []
(0..7).each {|i| opts.push([lblrow[i].chr(Encoding::UTF_8), valrow[i]]) }

And I ask why you people don't learn Haskell with it awesome method zipWith and check that Ruby has something similar, i.e. (writeing upper code in more rubish way):

opts = lblrow.map { |c| c.chr Encoding::UTF_8 }.zip(valrow)

Wow, one line, clear and awesome.