method
transpose
v1_9_3_125 -
Show latest stable
- Class:
Array
transpose()public
Assumes that self is an array of arrays and transposes the rows and columns.
a = [[1,2], [3,4], [5,6]] a.transpose #=> [[1, 3, 5], [2, 4, 6]]
static VALUE
rb_ary_transpose(VALUE ary)
{
long elen = -1, alen, i, j;
VALUE tmp, result = 0;
alen = RARRAY_LEN(ary);
if (alen == 0) return rb_ary_dup(ary);
for (i=0; i<alen; i++) {
tmp = to_ary(rb_ary_elt(ary, i));
if (elen < 0) { /* first element */
elen = RARRAY_LEN(tmp);
result = rb_ary_new2(elen);
for (j=0; j<elen; j++) {
rb_ary_store(result, j, rb_ary_new2(alen));
}
}
else if (elen != RARRAY_LEN(tmp)) {
rb_raise(rb_eIndexError, "element size differs (%ld should be %ld)",
RARRAY_LEN(tmp), elen);
}
for (j=0; j<elen; j++) {
rb_ary_store(rb_ary_elt(result, j), i, rb_ary_elt(tmp, j));
}
}
return result;
} Related methods
- Instance methods
- &
- *
- +
- -
- <<
- <=>
- ==
- []
- []=
- abbrev
- assoc
- at
- clear
- collect
- collect!
- combination
- compact
- compact!
- concat
- count
- cycle
- dclone
- delete
- delete_at
- delete_if
- drop
- drop_while
- each
- each_index
- empty?
- eql?
- fetch
- fill
- find_index
- first
- flatten
- flatten!
- frozen?
- hash
- include?
- index
- initialize_copy
- insert
- inspect
- join
- keep_if
- last
- length
- map
- map!
- pack
- permutation
- pop
- pretty_print
- pretty_print_cycle
- product
- push
- rassoc
- reject
- reject!
- repeated_combination
- repeated_permutation
- replace
- reverse
- reverse!
- reverse_each
- rindex
- rotate
- rotate!
- sample
- select
- select!
- shelljoin
- shift
- shuffle
- shuffle!
- size
- slice
- slice!
- sort
- sort!
- sort_by!
- take
- take_while
- to_a
- to_ary
- to_csv
- to_s
- transpose
- uniq
- uniq!
- unshift
- values_at
- zip
- |
- Class methods
- []
- new
- try_convert