Class: Matrices::Matrices
- Inherits:
-
Object
- Object
- Matrices::Matrices
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/Matrices/matrices.rb
Instance Attribute Summary collapse
-
#datos ⇒ Object
readonly
Returns the value of attribute datos.
-
#m ⇒ Object
readonly
Returns the value of attribute m.
-
#n ⇒ Object
readonly
Returns the value of attribute n.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Overwritting del metodo <=> para el modulo Comparable.
-
#each ⇒ Object
Overwritting del metodo each.
-
#initialize(m = 1, n = 1, opcion = 0) ⇒ Matrices
constructor
A new instance of Matrices.
-
#setRandom ⇒ Object
Metodo que inicia la matriz con valores aleatorios.
-
#to_s ⇒ Object
Overwritting del metodo to_s.
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
#datos ⇒ Object (readonly)
Returns the value of attribute datos.
6 7 8 |
# File 'lib/Matrices/matrices.rb', line 6 def datos @datos end |
#m ⇒ Object (readonly)
Returns the value of attribute m.
6 7 8 |
# File 'lib/Matrices/matrices.rb', line 6 def m @m end |
#n ⇒ Object (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 |
#each ⇒ Object
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 |
#setRandom ⇒ Object
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_s ⇒ Object
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 |