write a 2x3 integer-based matrix in ruby, and then show how to reverse it so that 1 2 3 in row 1, becomes 3 2 1, and 4 5 6 in row two becomes 6 5 4 in ruby

To create a 2x3 matrix in Ruby, you can use a nested array.

Here's an example of how to create a 2x3 matrix and then reverse it:

main.rb
matrix = [[1, 2, 3], [4, 5, 6]] # Creating a 2x3 matrix

reversed_matrix = matrix.map(&:reverse) # Reversing the matrix

# Printing the reversed matrix
reversed_matrix.each do |row|
  puts row.join(" ")
end
207 chars
9 lines

This code will output:

main.rb
3 2 1
6 5 4
12 chars
3 lines

The map method is used to iterate over each row of the matrix and apply the reverse method to reverse the elements within each row. Finally, the join method is used to join the elements of each row with a space separator before printing them.

related categories

gistlibby LogSnag