Class: Alimento

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

Direct Known Subclasses

Grupo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, protein, glucid, lipid) ⇒ Alimento

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



8
9
10
11
12
13
# File 'lib/pract.rb', line 8

def initialize (name, protein, glucid, lipid)
@name = name
@protein = protein
@glucid = glucid
		@lipid = lipid
end

Instance Attribute Details

#glucidObject

Returns the value of attribute glucid



4
5
6
# File 'lib/pract.rb', line 4

def glucid
  @glucid
end

#lipidObject

Returns the value of attribute lipid



4
5
6
# File 'lib/pract.rb', line 4

def lipid
  @lipid
end

#nameObject

Returns the value of attribute name



4
5
6
# File 'lib/pract.rb', line 4

def name
  @name
end

#proteinObject

Returns the value of attribute protein



4
5
6
# File 'lib/pract.rb', line 4

def protein
  @protein
end

Instance Method Details

#<=>(other) ⇒ Object

Make it comparable



39
40
41
# File 'lib/pract.rb', line 39

def <=> (other)
  return self.energy <=> other.energy
end

#==(other) ⇒ Object

Define also the function == in comparable



43
44
45
# File 'lib/pract.rb', line 43

def == (other)
  return self.energy == other.energy
end

#energyObject

Returns the total energy of food

Returns:

  • the total energy of food



27
28
29
# File 'lib/pract.rb', line 27

def energy
	return glucid_energy + protein_energy + lipid_energy
end

#glucid_energyObject

Returns the energy provided by glucids

Returns:

  • the energy provided by glucids



19
20
21
# File 'lib/pract.rb', line 19

def glucid_energy
	return @glucid * 4
end

#lipid_energyObject

Returns the energy provided by lipids

Returns:

  • the energy provided by lipids



23
24
25
# File 'lib/pract.rb', line 23

def lipid_energy
	return @lipid * 9
end

#protein_energyObject

Returns the energy provided by proteins

Returns:

  • the energy provided by proteins



15
16
17
# File 'lib/pract.rb', line 15

def protein_energy
	return @protein * 4
end

#show_energyObject

Returns only name and total energy

Returns:

  • only name and total energy



35
36
37
# File 'lib/pract.rb', line 35

def show_energy
	return "#{@name}: #{self.energy.round(2)} Kcal."
end

#to_sObject

Returns the format showed by puts

Returns:

  • the format showed by puts



31
32
33
# File 'lib/pract.rb', line 31

def to_s
	return "#{@name}: #{@protein}g proteinas, #{@glucid}g glucidos, #{@lipid}g grasas"
end