Class: Array

Inherits:
Object
  • Object
show all
Defined in:
Alimentos.rb

Overview

Abrimos la clase Array para añadir los métodos de ordenación.

Instance Method Summary collapse

Instance Method Details

#ordenar_eachObject

metodo que ordena un array mediante el método each.

Returns:

  • Un nuevo array con los datos ordenados según el valor energético.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'Alimentos.rb', line 118

def ordenar_each
   solucion=Array.new
   solucion << self[0]
   
   self.drop(1).each do |val_1|
      solucion.each_with_index do |val_2,i|
         if val_1<=val_2
            solucion.insert(i,val_1)
            break
         end
         
         if i==solucion.length-1
            solucion << val_1
            break
         end
      end
   end
   solucion
end

#ordenar_forObject

metodo que ordena un array mediante bucles for.

Returns:

  • Un nuevo array con los datos ordenados según el valor energético.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'Alimentos.rb', line 97

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