Class: Alimento

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/pract.rb

Direct Known Subclasses

Grupo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, protein, glucid, lipid, glucose) ⇒ Alimento

Constructor @ param name of food [String] and the attributes: protein, glucid, lipid



9
10
11
12
13
14
15
# File 'lib/pract.rb', line 9

def initialize (name, protein, glucid, lipid, glucose)
@name = name
@protein = protein
@glucid = glucid
    @lipid = lipid
   @glucose = glucose
end

Instance Attribute Details

#glucidObject

Returns the value of attribute glucid.



5
6
7
# File 'lib/pract.rb', line 5

def glucid
  @glucid
end

#glucoseObject

Returns the value of attribute glucose.



5
6
7
# File 'lib/pract.rb', line 5

def glucose
  @glucose
end

#lipidObject

Returns the value of attribute lipid.



5
6
7
# File 'lib/pract.rb', line 5

def lipid
  @lipid
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/pract.rb', line 5

def name
  @name
end

#proteinObject

Returns the value of attribute protein.



5
6
7
# File 'lib/pract.rb', line 5

def protein
  @protein
end

Instance Method Details

#<=>(other) ⇒ Object

Make it comparable



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

def <=> (other)
  return self.energy <=> other.energy
end

#==(other) ⇒ Object

Define also the function == in comparable



45
46
47
# File 'lib/pract.rb', line 45

def == (other)
  return self.energy == other.energy
end

#aibcObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pract.rb', line 49

def aibc
   aux = @glucose.each_with_index.map do # i es el value , m es el indice
      |i, m| @glucose[m].each_with_index.map do
         |i, n|
           if(n>0)
             i-@glucose[m][0] + (@glucose[m][n-1]-@glucose[m][0])
           else
             0
           end
        end.each.collect{|i| i.round(2)/2*5}.reduce('+')
     end
   return aux
end

#energyObject

Returns the total energy of food.

Returns:

  • the total energy of food



29
30
31
# File 'lib/pract.rb', line 29

def energy
  return glucid_energy + protein_energy + lipid_energy
end

#glucid_energyObject

Returns the energy provided by glucids.

Returns:

  • the energy provided by glucids



21
22
23
# File 'lib/pract.rb', line 21

def glucid_energy
  return @glucid * 4
end

#lipid_energyObject

Returns the energy provided by lipids.

Returns:

  • the energy provided by lipids



25
26
27
# File 'lib/pract.rb', line 25

def lipid_energy
  return @lipid * 9
end

#protein_energyObject

Returns the energy provided by proteins.

Returns:

  • the energy provided by proteins



17
18
19
# File 'lib/pract.rb', line 17

def protein_energy
  return @protein * 4
end

#show_energyObject

Returns only name and total energy.

Returns:

  • only name and total energy



37
38
39
# File 'lib/pract.rb', line 37

def show_energy
  return "#{@name}: #{self.energy.round(2)} Kcal."
end

#to_sObject

Returns the format showed by puts.

Returns:

  • the format showed by puts



33
34
35
# File 'lib/pract.rb', line 33

def to_s
  return "#{@name}: #{@protein}g proteinas, #{@glucid}g glucidos, #{@lipid}g grasas"
end