Class: Matrices::Matrices

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/Matrices/matrices.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(m = 1, n = 1, opcion = 0) ⇒ Matrices

Returns a new instance of Matrices.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/Matrices/matrices.rb', line 7

def initialize(m=1,n=1,opcion=0)
    @m,@n = m,n
    @datos = Array.new(m)
        for i in 0...@n do
            @datos[i] = Array.new(@n)
        end
    if (opcion == 1)
    #Rellenamos la matriz con valores aletarios
        setRandom
    else
        for i in 0...opcion.size do
            @datos[i] = opcion[i]
        end
    end
    #Imprimimos la matriz
    puts(self)
end

Instance Attribute Details

#datosObject (readonly)

Returns the value of attribute datos.



6
7
8
# File 'lib/Matrices/matrices.rb', line 6

def datos
  @datos
end

#mObject (readonly)

Returns the value of attribute m.



6
7
8
# File 'lib/Matrices/matrices.rb', line 6

def m
  @m
end

#nObject (readonly)

Returns the value of attribute n.



6
7
8
# File 'lib/Matrices/matrices.rb', line 6

def n
  @n
end

Instance Method Details

#<=>(other) ⇒ Object

Overwritting del metodo <=> para el modulo Comparable



41
42
43
# File 'lib/Matrices/matrices.rb', line 41

def <=>(other)
    return (@m <=> other.m) && (@n <=> other.n)
end

#eachObject

Overwritting del metodo each



33
34
35
36
37
38
39
# File 'lib/Matrices/matrices.rb', line 33

def each
    for i in 0...@m do
     for j in 0...@n do
        yield @datos[i][j]
     end
    end
end

#setRandomObject

Metodo que inicia la matriz con valores aleatorios



25
26
27
28
29
30
31
# File 'lib/Matrices/matrices.rb', line 25

def setRandom
    for i in 0...@m do
     for j in 0...@n do
        @datos[i][j] = rand(100)
     end
    end
end

#to_sObject

Overwritting del metodo to_s



45
46
47
48
49
50
51
52
53
54
# File 'lib/Matrices/matrices.rb', line 45

def to_s
    resultado = "[ "
    for i in 0...@m do
     for j in 0...@n do
       resultado << "  #{@datos[i][j]}  "
     end
    end
    resultado << " ]"
    resultado
end