Class: Alimento

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/prct06/valor_calorico.rb

Overview

Author:

  • Cristina Garrido Amador

Direct Known Subclasses

AlimentoC

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns Lista type of the class.

Parameters:

  • name,

    proteinas, glucidos, lipidos [String], [Number], [Number], [Number]



17
18
19
20
21
22
# File 'lib/prct06/valor_calorico.rb', line 17

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

Instance Attribute Details

#datosObject

Includes the module Comparable It have four values,name of the aliment, and the information of proteins, lipids and glucids



10
11
12
# File 'lib/prct06/valor_calorico.rb', line 10

def datos
  @datos
end

#glucidosObject

Includes the module Comparable It have four values,name of the aliment, and the information of proteins, lipids and glucids



10
11
12
# File 'lib/prct06/valor_calorico.rb', line 10

def glucidos
  @glucidos
end

#lipidosObject

Includes the module Comparable It have four values,name of the aliment, and the information of proteins, lipids and glucids



10
11
12
# File 'lib/prct06/valor_calorico.rb', line 10

def lipidos
  @lipidos
end

#nombreObject

Includes the module Comparable It have four values,name of the aliment, and the information of proteins, lipids and glucids



10
11
12
# File 'lib/prct06/valor_calorico.rb', line 10

def nombre
  @nombre
end

#proteinasObject

Includes the module Comparable It have four values,name of the aliment, and the information of proteins, lipids and glucids



10
11
12
# File 'lib/prct06/valor_calorico.rb', line 10

def proteinas
  @proteinas
end

Instance Method Details

#<=>(other) ⇒ Object

Returns Alimento type of the class.

Parameters:

Returns:

  • Alimento type of the class



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/prct06/valor_calorico.rb', line 28

def <=>(other) #operador !=
  return nil unless other.is_a?Alimento #devuelve nil si no son de la clase Alimento los dos objetos
  if valor_calorico == other.valor_calorico
    return 0
  else
    if valor_calorico < other.valor_calorico
	return -1 #devuelve true cuando es menor, y lo niega para conseguirlo negado, el operador < solo comprueba el menor
    else
	return 1 #devuelve true si es >
    end
  end
end

#==(other) ⇒ Object

Returns Alimento type of the class.

Parameters:

Returns:

  • Alimento type of the class



44
45
46
47
48
49
50
# File 'lib/prct06/valor_calorico.rb', line 44

def == (other)
    if other.is_a?Alimento
        valor_calorico == other.valor_calorico
    else
        false
    end
end

#AIBC(indice) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/prct06/valor_calorico.rb', line 60

def AIBC(indice) #indice = individuo
  aux=[]
  #zip --> coge dos vectores y genera pares, la pos 1 del vector 1 y la pos 2 del vector a los devuelve y los guardo en aux
  #de esta forma tendre en x la pos actual y en y la pos-1 para llevar a cabo la formula
  datos[indice][1..datos[indice].length - 1].zip(datos[indice][0..datos[indice].length - 2]) do |x,y|
    if x < datos[indice][0]
	aux << 0.0
    else
	aux << (((x-datos[indice][0])+(y-datos[indice][0]))/2)*5
    end
  end
  aux.reduce(:+) #sumatorio del vector auxiliar, es lo que devuelve el metodo
end

#to_sString

Returns the resulting of join all the information, name, proteins, glucids and lipids.

Returns:

  • (String)

    the resulting of join all the information, name, proteins, glucids and lipids



53
54
55
# File 'lib/prct06/valor_calorico.rb', line 53

def to_s
      imprime = "#{@nombre}: #{@proteinas} #{@glucidos} #{@lipidos}"
end

#valor_caloricoObject



76
77
78
# File 'lib/prct06/valor_calorico.rb', line 76

def valor_calorico
    valor = (@proteinas * 4) + (@glucidos * 4) + (@lipidos * 9)
end