Class: Alimento
Direct Known Subclasses
Instance Attribute Summary collapse
-
#glucid ⇒ Object
Returns the value of attribute glucid.
-
#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.
-
#energy ⇒ Object
The total energy of food.
-
#glucid_energy ⇒ Object
The energy provided by glucids.
-
#initialize(name, protein, glucid, lipid) ⇒ 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) ⇒ Alimento
Constructor @ param name of food [String] and the attributes: protein, glucid, lipid
8 9 10 11 12 13 |
# File 'lib/pract.rb', line 8 def initialize (name, protein, glucid, lipid) @name = name @protein = protein @glucid = glucid @lipid = lipid end |
Instance Attribute Details
#glucid ⇒ Object
Returns the value of attribute glucid
4 5 6 |
# File 'lib/pract.rb', line 4 def glucid @glucid end |
#lipid ⇒ Object
Returns the value of attribute lipid
4 5 6 |
# File 'lib/pract.rb', line 4 def lipid @lipid end |
#name ⇒ Object
Returns the value of attribute name
4 5 6 |
# File 'lib/pract.rb', line 4 def name @name end |
#protein ⇒ Object
Returns the value of attribute protein
4 5 6 |
# File 'lib/pract.rb', line 4 def protein @protein end |
Instance Method Details
#<=>(other) ⇒ Object
Make it comparable
39 40 41 |
# File 'lib/pract.rb', line 39 def <=> (other) return self.energy <=> other.energy end |
#==(other) ⇒ Object
Define also the function == in comparable
43 44 45 |
# File 'lib/pract.rb', line 43 def == (other) return self.energy == other.energy end |
#energy ⇒ Object
Returns the total energy of food
27 28 29 |
# File 'lib/pract.rb', line 27 def energy return glucid_energy + protein_energy + lipid_energy end |
#glucid_energy ⇒ Object
Returns the energy provided by glucids
19 20 21 |
# File 'lib/pract.rb', line 19 def glucid_energy return @glucid * 4 end |
#lipid_energy ⇒ Object
Returns the energy provided by lipids
23 24 25 |
# File 'lib/pract.rb', line 23 def lipid_energy return @lipid * 9 end |
#protein_energy ⇒ Object
Returns the energy provided by proteins
15 16 17 |
# File 'lib/pract.rb', line 15 def protein_energy return @protein * 4 end |
#show_energy ⇒ Object
Returns only name and total energy
35 36 37 |
# File 'lib/pract.rb', line 35 def show_energy return "#{@name}: #{self.energy.round(2)} Kcal." end |
#to_s ⇒ Object
Returns the format showed by puts
31 32 33 |
# File 'lib/pract.rb', line 31 def to_s return "#{@name}: #{@protein}g proteinas, #{@glucid}g glucidos, #{@lipid}g grasas" end |