Class: Alimento
Direct Known Subclasses
Instance Attribute Summary collapse
-
#glucid ⇒ Object
Returns the value of attribute glucid.
-
#glucose ⇒ Object
Returns the value of attribute glucose.
-
#lipid ⇒ Object
Returns the value of attribute lipid.
-
#name ⇒ Object
Returns the value of attribute name.
-
#protein ⇒ Object
Returns the value of attribute protein.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Make it comparable.
-
#==(other) ⇒ Object
Define also the function == in comparable.
- #aibc ⇒ Object
-
#energy ⇒ Object
The total energy of food.
-
#glucid_energy ⇒ Object
The energy provided by glucids.
-
#initialize(name, protein, glucid, lipid, glucose) ⇒ Alimento
constructor
Constructor @ param name of food [String] and the attributes: protein, glucid, lipid.
-
#lipid_energy ⇒ Object
The energy provided by lipids.
-
#protein_energy ⇒ Object
The energy provided by proteins.
-
#show_energy ⇒ Object
Only name and total energy.
-
#to_s ⇒ Object
The format showed by puts.
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
#glucid ⇒ Object
Returns the value of attribute glucid.
5 6 7 |
# File 'lib/pract.rb', line 5 def glucid @glucid end |
#glucose ⇒ Object
Returns the value of attribute glucose.
5 6 7 |
# File 'lib/pract.rb', line 5 def glucose @glucose end |
#lipid ⇒ Object
Returns the value of attribute lipid.
5 6 7 |
# File 'lib/pract.rb', line 5 def lipid @lipid end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/pract.rb', line 5 def name @name end |
#protein ⇒ Object
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 |
#aibc ⇒ Object
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 |
#energy ⇒ Object
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_energy ⇒ Object
Returns the energy provided by glucids.
21 22 23 |
# File 'lib/pract.rb', line 21 def glucid_energy return @glucid * 4 end |
#lipid_energy ⇒ Object
Returns the energy provided by lipids.
25 26 27 |
# File 'lib/pract.rb', line 25 def lipid_energy return @lipid * 9 end |
#protein_energy ⇒ Object
Returns the energy provided by proteins.
17 18 19 |
# File 'lib/pract.rb', line 17 def protein_energy return @protein * 4 end |
#show_energy ⇒ Object
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_s ⇒ Object
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 |