Class: Alimento

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

Overview

Class Alimento Stores a food and some values of its elements.

Direct Known Subclasses

Carbohidratado, Carnido, Fruta, Graso, OvoLacteo, Pescado, Verdura

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, proteins, glucids, lipids) ⇒ Alimento

Initialization of the object given its attributes

Parameters:

  • name (String)

    Name of the Alimento

  • proteins (Fixnum)

    Proteins ammount of the Alimento

  • glucids (Fixnum)

    Glucids ammount of the Alimento

  • lipids (Fixnum)

    Lipids ammount of the Alimento



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

#glucidsObject (readonly)

Returns the value of attribute glucids.



13
14
15
# File 'lib/nutrientes/alimento.rb', line 13

def glucids
  @glucids
end

#lipidsObject (readonly)

Returns the value of attribute lipids.



13
14
15
# File 'lib/nutrientes/alimento.rb', line 13

def lipids
  @lipids
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/nutrientes/alimento.rb', line 13

def name
  @name
end

#proteinsObject (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

Parameters:

  • another (Object)

    Object to compare with

Returns:

  • (Fixnum)

    1 if the first object is bigger, 0 if equal and -1 if minor



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_valueFixnum

Calculates the caloric value of the Alimento

Returns:

  • (Fixnum)

    The caloric value



70
71
72
# File 'lib/nutrientes/alimento.rb', line 70

def caloric_value()
   return (@proteins * 4 + @lipids * 9 + @glucids * 4)
end

#to_sString

Method to transform an object of Alimento to a string

Returns:

  • (String)

    the object as 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