Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/prct06/array.rb

Overview

Se definen nuevos métodos para la clase Array por defecto Su desarrollo ha sido dirigido por pruebas (TDD).

Author

Sergio Ferrera de Diego (sergioferrera1296@gmail.com)

Copyright

Cretive Commons

License

Distributes under the same terms as Ruby

Instance Method Summary collapse

Instance Method Details

#ordenar_eachObject

Método para ordenador por inserción un array mediante método each



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/prct06/array.rb', line 29

def ordenar_each
    ordenado=Array.new
    ordenado << self[0]
    self.drop(1).each do |x|
        ordenado.each_with_index do |y,i|
            if x>=y
                ordenado.insert(i,x)
                break
            end
            if i==ordenado.length-1
                ordenado << x
                break
            end
        end
    end
    ordenado
end

#ordenar_forObject

Método para ordenador por inserción un array mediante bucles for



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/prct06/array.rb', line 11

def ordenar_for
    ordenado=Array.new
    ordenado << self[0]
    for i in (1..self.length-1)
        for j in (0..ordenado.length-1)
            if self[i]>=ordenado[j]
                ordenado.insert(j,self[i])
                break
            end
            if j==ordenado.length-1
                ordenado << self[i]
            end
        end
    end
    ordenado
end