Class: Array

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

Instance Method Summary collapse

Instance Method Details

#each_helperObject



52
53
54
# File 'lib/prct06/array.rb', line 52

def each_helper
       return self.collect{|food| food.energy;}.reduce(:+).round(2)
end

#eachsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/prct06/array.rb', line 21

def eachs
       sorted = [self[0]]
       self.each_with_index do |x, pos_x|
           if (pos_x != 0)
               sorted.each_with_index do |y, pos_y|
                   if (pos_y == sorted.size-1)
                       if (x.each_helper < y.each_helper)
                           sorted.insert(pos_y, x)
                           break
                       else
                           sorted.push(x)
                           break
                       end
                   elsif (x.each_helper < y.each_helper)
                       sorted.insert(pos_y, x)
                       break
                   end
               end
           end
       end
       return sorted
end

#for_helperObject



44
45
46
47
48
49
50
# File 'lib/prct06/array.rb', line 44

def for_helper
       sum = 0
       for i in (0...self.size)
           sum += self[i].energy
       end
       return sum
end

#forsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/prct06/array.rb', line 5

def fors
       sorted = [self[0]]
       for i in (1...self.size)
           act = self[i]
           for j in (0..sorted.size)
               if (j == sorted.size)
                   sorted.push(act)
               elsif (act.for_helper < sorted[j].for_helper)
                   sorted.insert(j, act)
                   break
               end
           end
       end
       return sorted
end