Class: Alimento::Alimentos

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

Direct Known Subclasses

AlimentoClas

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nombre, proteinas, glucidos, lipidos) ⇒ Alimentos

Se asigna el nombre, y la cantidad de proteínas, glúcidos y lípidos del alimento



17
18
19
20
21
22
23
# File 'lib/alimento/alimentos.rb', line 17

def initialize(nombre, proteinas, glucidos, lipidos)
  @nombre = nombre
  @proteinas = proteinas
  @glucidos = glucidos
  @lipidos = lipidos

end

Instance Attribute Details

#glucidosObject (readonly)

Returns the value of attribute glucidos.



14
15
16
# File 'lib/alimento/alimentos.rb', line 14

def glucidos
  @glucidos
end

#lipidosObject (readonly)

Returns the value of attribute lipidos.



14
15
16
# File 'lib/alimento/alimentos.rb', line 14

def lipidos
  @lipidos
end

#nombreObject (readonly)

Returns the value of attribute nombre.



14
15
16
# File 'lib/alimento/alimentos.rb', line 14

def nombre
  @nombre
end

#proteinasObject (readonly)

Returns the value of attribute proteinas.



14
15
16
# File 'lib/alimento/alimentos.rb', line 14

def proteinas
  @proteinas
end

Instance Method Details

#<=>(another) ⇒ Object

Incluido para poder utilizar los métodos del mixin Comparable



39
40
41
42
43
44
45
46
# File 'lib/alimento/alimentos.rb', line 39

def <=>(another)
  compare = calcValEn <=> another.calcValEn
  if compare == 0
    return @nombre <=> another.nombre
  else
    return compare
  end
end

#calcValEnObject

Calcula y devuelve el valor energético del alimento



34
35
36
# File 'lib/alimento/alimentos.rb', line 34

def calcValEn
  (@proteinas * VAL_EN_PROTEINAS) + (@glucidos * VAL_EN_GLUCIDOS) + (@lipidos * VAL_EN_LIPIDOS)
end

#to_sObject

Operador de salida de la clase alimento. Muestra el alimento con formato: Nombre Cantidad_de_Proteínas Cantidad_de_glúcidos Cantidad_de_lípidos Ej: Huevos fritos 14.1 0.0 19.5



29
30
31
# File 'lib/alimento/alimentos.rb', line 29

def to_s
  "#{@nombre}\t#{@proteinas}\t#{@glucidos}\t#{@lipidos}"
end