O trace() é um método embutido em Ruby que retorna o trace, ou seja, a soma dos elementos diagonais da array.

Sintaxe : mat1.trace()

Parâmetros : A função precisa da array cujo traço deve ser retornado.

Valor de retorno : ele retorna o rastreamento.

Exemplo 1 :

# Ruby program for trace() method in Matrix
  
# Include matrix 
require "matrix"
  
# Initialize a matrix 
mat1 = Matrix[[3, 12], [2, 8]]  
  
# Prints the trace
puts  mat1.trace()

Produto :

11

Exemplo 2 :

# Ruby program for trace() method in Matrix
  
# Include matrix 
require "matrix"
  
# Initialize a matrix 
mat1 = Matrix[[1, 0, 6], [6, 1, 7], [1, 2, 19]]   
  
# Prints the trace
puts  mat1.trace()

Produto :

21