class Comida

Esta clase permite ver los distintos componentes de un alimento y su valor energético Se ha incluido el mixin Comparable

Attributes

carbohydrates[R]

Getters de las distintas variables de instancia

g[RW]
lipids[R]

Getters de las distintas variables de instancia

name[R]

Getters de las distintas variables de instancia

proteins[R]

Getters de las distintas variables de instancia

Public Class Methods

new(name_value, proteins_value, carbohydrates_value, lipids_value) click to toggle source

Se asignan los valores que debe tener el alimento, es decir, su nombre, numero de proteinas, glúcidos (carbohidratos) y lípidos

# File lib/Alimento/Comida.rb, line 17
def initialize (name_value, proteins_value, carbohydrates_value, lipids_value)
  @name = name_value
  @proteins = proteins_value
  @carbohydrates = carbohydrates_value
  @lipids = lipids_value
  @g = nil
end

Public Instance Methods

<=>(other_food) click to toggle source

Se define para incluir el mixin comparable Se toma como valor para la comparación el valor energético y para saber si son iguales también se toma el nombre.

# File lib/Alimento/Comida.rb, line 69
def <=>(other_food) 
  return nil unless other_food.is_a?Comida
  val_energ <=> other_food.val_energ
end
==(other_food) click to toggle source

para comparar se debe pasar otra comida como argumento

# File lib/Alimento/Comida.rb, line 75
def ==(other_food)
  return nil unless other_food.is_a?Comida
  (@name == other_food.name) && (val_energ == other_food.val_energ)
end
aibc_funcional() click to toggle source
# File lib/Alimento/Comida.rb, line 115
  def aibc_funcional
    
    r =[]
    arr = []
    @g.collect{
      |x|
      x.each_with_index{|y, index|
        if(index != 0)
          if(y<x[0])
            arr[index-1]=0.0
          else
            arr[index-1] = ((((x[index]-x[0]) + (x[index-1]-x[0]))/2)*5)               
          end          
        end
      }
      r.push(arr.reduce(:+))
    }
    r

=begin
    ge = (0...@g[0].size).to_a.zip(@g[0], @g[1])
    puts "#{ge}"
    @g.each_with_index{
      |x, index| 
      if (index != 0)
        if (x[index] < x[0])
          (((x[index]-@g[0]) + (x[index-1]-x[0]))/2)*5
        end
      end
     }
=end
  end
aibc_imperative() click to toggle source
# File lib/Alimento/Comida.rb, line 81
def aibc_imperative
  i = 0
  r = []
  while i < @g.size
    index = 1
    s = []
    while index < @g[i].size
      if @g[i][index] < @g[i][0]
      s << 0.0
      else
      s << (((@g[i][index] - @g[i][0]) + (@g[i][index-1] - @g[i][0]))/2)*5
      end
      index = index + 1
    end
    r << s
    i = i + 1
  end

  suma = []
  j = 0
  while j < @g.size
    k = 0
    s = 0
    while k < r[j].size
      s = s + r[j][k]
      k = k + 1
    end
    suma << s
    j = j + 1
  end
  suma
end
format_ch() click to toggle source

Función que devuelve las kcalorias del alimento por su número de glúcidos

# File lib/Alimento/Comida.rb, line 41
def format_ch
  ch_kcal = @carbohydrates * 4
  return ch_kcal
end
format_lipids() click to toggle source

Función que devuelve las kcalorias del alimento por su número de lípidos

# File lib/Alimento/Comida.rb, line 47
def format_lipids
  lipids_kcal = @lipids * 9
  return lipids_kcal
end
format_proteins() click to toggle source

Función que devuelve las kcalorias del alimento por su número de proteinas

# File lib/Alimento/Comida.rb, line 35
def format_proteins
  proteins_kcal = @proteins * 4
  return proteins_kcal
end
show_ev() click to toggle source

Metodo que convierte a string los componentes del alimento en kcaloría y su valor energético total

# File lib/Alimento/Comida.rb, line 62
def show_ev
  "The fortmated values are: p->#{format_proteins}, c->#{format_ch}, l->#{format_lipids}; The energ. value is -> #{val_energ} kcal"
end
to_s() click to toggle source

Función to_string

# File lib/Alimento/Comida.rb, line 28
def to_s
  "p: #{@proteins}, c: #{@carbohydrates}, l: #{@lipids}"
end
val_energ() click to toggle source

Función que devuelve las kcalorias totales del alimento

# File lib/Alimento/Comida.rb, line 53
def val_energ
  @energ_val = format_proteins + format_ch + format_lipids
  return @energ_val
end