class Alimento

Attributes

glucid[RW]
glucose[RW]
lipid[RW]
name[RW]
protein[RW]

Public Class Methods

new(name, protein, glucid, lipid, glucose) click to toggle source

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

# File lib/pract.rb, line 9
  def initialize (name, protein, glucid, lipid, glucose)
          @name = name
          @protein = protein
          @glucid = glucid
          @lipid = lipid
@glucose = glucose
  end

Public Instance Methods

<=>(other) click to toggle source

Make it comparable

# File lib/pract.rb, line 41
def <=> (other)
  return self.energy <=> other.energy
end
==(other) click to toggle source

Define also the function == in comparable

# File lib/pract.rb, line 45
def == (other)
  return self.energy == other.energy
end
aibc() click to toggle source
# 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() click to toggle source

@return the total energy of food

# File lib/pract.rb, line 29
def energy
        return glucid_energy + protein_energy + lipid_energy
end
glucid_energy() click to toggle source

@return the energy provided by glucids

# File lib/pract.rb, line 21
def glucid_energy
        return @glucid * 4
end
lipid_energy() click to toggle source

@return the energy provided by lipids

# File lib/pract.rb, line 25
def lipid_energy
        return @lipid * 9
end
protein_energy() click to toggle source

@return the energy provided by proteins

# File lib/pract.rb, line 17
def protein_energy
        return @protein * 4
end
show_energy() click to toggle source

@return only name and total energy

# File lib/pract.rb, line 37
def show_energy
        return "#{@name}: #{self.energy.round(2)} Kcal."
end
to_s() click to toggle source

@return the format showed by puts

# File lib/pract.rb, line 33
def to_s
        return "#{@name}: #{@protein}g proteinas, #{@glucid}g glucidos, #{@lipid}g grasas"
end