How to read 2-dimensional arrays in Ruby?

1

I am learning to program in ruby, I already knew how to program in c ++ and with the use of cin there is no problem but in ruby I have noticed that the ruby gets read a complete line so that if I enter:

1 2 3 4

5 6 7 8

I can not read what comes after the 1 or the 5 Is there any extra reading function or do we have to take the complete String that returns gets and go through it to get the integers

    
asked by Elias Segundo 18.05.2016 в 14:12
source

2 answers

2

The gets method reads a complete line. You will have to extract each element of the read line and convert them into a number.

In a piggy plan:

gets.split.map {|x| x.to_i}
    
answered by 18.05.2016 в 14:31
0

Also with Matrix you can do matrix operations

require 'matrix'  
m = Matrix[
 [1, 2, 3, 4],
 [5, 6, 7, 8]
]

It's already installed with ruby, it's just importing the class. And you can use each vector as normal Arrays.

Example:

m.minor(0..1, 2..2) 
# => Matrix[[3], [7]]

m.column 0
#=> Vector[1,5]

m.row 0
#=> Vector[1,2,3,4]
    
answered by 05.08.2016 в 18:08