Gavin Kistner
1/22/2007 10:52:00 PM
Mike Keller wrote:
> Alex Young wrote:
> > a.zip(b).map{|c| c.flatten}
>
> This causes a different problem, what this does is format the output
> like:
> 123
> 4
Actually, it doesn't 'format' the output at all. Let's see what's going
on:
irb(main):002:0> a = %w{123 456 789 }
=> ["123", "456", "789"]
irb(main):003:0> b = %w{x y z}
=> ["x", "y", "z"]
irb(main):004:0> a.zip(b)
=> [["123", "x"], ["456", "y"], ["789", "z"]]
OK, so zipping the two arrays together produces an array, where each
piece is itself an array of the two values.
irb(main):005:0> a.zip(b).map{ |c| c.flatten }
=> [["123", "x"], ["456", "y"], ["789", "z"]]
Hrm...so this doesn't do anything, because they individual pieces were
already flattened.
irb(main):006:0> puts ["1", "2"]
1
2
irb(main):007:0> puts a.zip(b)[0]
123
x
Ah, when you pass an array to "puts", it puts each part of the array on
each line. (That's what you're seeing.)
Let's fix that.
irb(main):008:0> a.zip(b).map{ |pair| pair.join }
=> ["123x", "456y", "789z"]
irb(main):009:0> puts a.zip(b).map{ |pair| pair.join }
123x
456y
789z
There you go. :)