Transpose

This occurred to me while lying sick in bed…

[email protected] /cygdrive/c/Temp
$ ruby dd.rb
[[“foo”, “bar”, “baz”, “biz”], [1, 2, 3, 4], [100, 200, 300, 400]]
[[“foo”, 1, 100], [“bar”, 2, 200], [“baz”, 3, 300], [“biz”, 4, 400]]

[email protected] /cygdrive/c/Temp
$ cat dd.rb

require ‘enumerator’
require ‘pp’

def transpose(a)
a.inject {|aa,b| aa.to_enum(:zip, b)}.inject([]){|aa,b| aa <<
b.flatten}
end

a = [
%w{foo bar baz biz},
[1,2,3,4],
[100,200,300,400],
]

pp a, transpose(a)

Did we have this before?

Whatever…

robert

On 1/31/07, Robert K. [email protected] wrote:

[100,200,300,400],
]

pp a, transpose(a)

Did we have this before?

Whatever…

a = [
%w{foo bar baz biz},
[1,2,3,4],
[100,200,300,400],
]

a.transpose

Blessings,
TwP

Robert,

It’s actually a built in method on array.

a.transpose will get you the same result.
Nice solution, however.

–Tyler

On Thu, 1 Feb 2007, Robert K. wrote:

This occurred to me while lying sick in bed…

sick being the operative word!

:wink:

-a

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs