Class: Alimento
- Inherits:
-
Object
- Object
- Alimento
- Includes:
- Comparable
- Defined in:
- lib/nutrientes/alimento.rb
Overview
Class Alimento Stores a food and some values of its elements.
Instance Attribute Summary collapse
-
#glucids ⇒ Object
readonly
Returns the value of attribute glucids.
-
#lipids ⇒ Object
readonly
Returns the value of attribute lipids.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#proteins ⇒ Object
readonly
Returns the value of attribute proteins.
Instance Method Summary collapse
-
#<=>(another) ⇒ Fixnum
Method to implement the comparison between two Alimentos.
-
#caloric_value ⇒ Fixnum
Calculates the caloric value of the Alimento.
-
#initialize(name, proteins, glucids, lipids) ⇒ Alimento
constructor
Initialization of the object given its attributes.
-
#to_s ⇒ String
Method to transform an object of Alimento to a string.
Constructor Details
#initialize(name, proteins, glucids, lipids) ⇒ Alimento
Initialization of the object given its attributes
55 56 57 58 59 60 |
# File 'lib/nutrientes/alimento.rb', line 55 def initialize (name, proteins, glucids, lipids) @name = name @proteins = proteins @glucids = glucids @lipids = lipids end |
Instance Attribute Details
#glucids ⇒ Object (readonly)
Returns the value of attribute glucids.
13 14 15 |
# File 'lib/nutrientes/alimento.rb', line 13 def glucids @glucids end |
#lipids ⇒ Object (readonly)
Returns the value of attribute lipids.
13 14 15 |
# File 'lib/nutrientes/alimento.rb', line 13 def lipids @lipids end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/nutrientes/alimento.rb', line 13 def name @name end |
#proteins ⇒ Object (readonly)
Returns the value of attribute proteins.
13 14 15 |
# File 'lib/nutrientes/alimento.rb', line 13 def proteins @proteins end |
Instance Method Details
#<=>(another) ⇒ Fixnum
Method to implement the comparison between two Alimentos
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/nutrientes/alimento.rb', line 18 def <=>(another) if(another.caloric_value > caloric_value) return -1 else if(another.caloric_value < caloric_value) return 1 end if (another.proteins > proteins) return -1 else if(another.proteins < proteins) return 1 end if(another.glucids > glucids) return -1 else if(another.glucids < glucids) return 1 end if(another.lipids > lipids) return -1 else if(another.lipids < lipids) return 1 else return 0 end end end end end end |
#caloric_value ⇒ Fixnum
Calculates the caloric value of the Alimento
70 71 72 |
# File 'lib/nutrientes/alimento.rb', line 70 def caloric_value() return (@proteins * 4 + @lipids * 9 + @glucids * 4) end |
#to_s ⇒ String
Method to transform an object of Alimento to a string
64 65 66 |
# File 'lib/nutrientes/alimento.rb', line 64 def to_s() return String.new(@name + ", " + @proteins.to_s + "g proteins, " + @glucids.to_s + "g glucids, " + @lipids.to_s + "g lipids") end |