Array#transpose not transposing diagonal values

Hello fellow rubyists! I am having an issue with Array#transpose. I have noticed that when I am trying to transpose a 2d array, that has dimensions 3 * 3, If only the diagonal indices are populated, the array does not appear to have been transposed. I have inserted some pictures below. Please note that my class methods #print and #print_trans are simply printing the grid, and then printing the transposed grid. I was going to put in a picture of the opposite scenario, where only one column or row has been filled out and the array transposed correctly but since I am a new user I cannot upload >1 picture in my post.

Nevermind! Transpose does not alter diagonally positioned elements :sigh:

1 Like

Substitute the array #1 for the array #3. And Verci versa.

You haven’t explained clearly what a “transposed” grid is to you. Ruby’s transpose method flips the rows into columns, and therefore the columns into rows. Since each column is the same as each row in your array, transpose doesn’t appear to do anything.

I’ll take a wild stab at what you want and say that in your example you’d like the zeros to start at the top right and go to the bottom left. If so, reversing each row will do the trick:

p my_array.map(&:reverse)

I realized that what I in fact wanted, was a mirrored array. I wrote a method to do this for me and it worked very nicely. Thank you for your input! Sorry if my initial post wasn’t too clear haha, I am new to programming and still figuring things out!. :smiley:

Perhaps a “mirrored array” would have the zeros start at the top right and go to the bottom left? If so, reversing each row will do the trick. :slight_smile: No need to write your own method.

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