Class: Nutri

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

Overview

Manages all the informations a normal etiquett contains.

Author:

  • roro

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, fat, sfats, hydrates, sugar, protein, salt) ⇒ nil

initialize the obj

Parameters:

  • name (string)
    Name
  • fat (int)
    Fat value
  • sfats (int)
    Saturates Fat value
  • hydrates (int)
    Hydrates value
  • sugar (int)
    Sugar value
  • protein (int)
    Protein value
  • salt (int)
    Salt Value


37
38
39
40
41
# File 'lib/prct06/nutri.rb', line 37

def initialize(name, fat, sfats, hydrates, sugar, protein, salt)
	@name, @fat, @sfats, @hydrates, @sugar, @protein, @salt = name, fat, sfats, hydrates, sugar, protein, salt
	@energy = 0.0
	senergy()
end

Instance Attribute Details

#energyObject (readonly)

Returns the value of attribute energy.



13
14
15
# File 'lib/prct06/nutri.rb', line 13

def energy
  @energy
end

#fatObject (readonly)

Returns the value of attribute fat.



13
14
15
# File 'lib/prct06/nutri.rb', line 13

def fat
  @fat
end

#hydratesObject (readonly)

Returns the value of attribute hydrates.



13
14
15
# File 'lib/prct06/nutri.rb', line 13

def hydrates
  @hydrates
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/prct06/nutri.rb', line 13

def name
  @name
end

#proteinObject (readonly)

Returns the value of attribute protein.



13
14
15
# File 'lib/prct06/nutri.rb', line 13

def protein
  @protein
end

#saltObject (readonly)

Returns the value of attribute salt.



13
14
15
# File 'lib/prct06/nutri.rb', line 13

def salt
  @salt
end

#sfatsObject (readonly)

Returns the value of attribute sfats.



13
14
15
# File 'lib/prct06/nutri.rb', line 13

def sfats
  @sfats
end

#sugarObject (readonly)

Returns the value of attribute sugar.



13
14
15
# File 'lib/prct06/nutri.rb', line 13

def sugar
  @sugar
end

Instance Method Details

#<=>(other) ⇒ bool

Defines wich value should be compared

Parameters:

  • other (object)
    another object

Returns:

  • (bool)
    True or False whether the compatrison is correct or not.


21
22
23
# File 'lib/prct06/nutri.rb', line 21

def <=>(other)
	@name <=> other.name
end

#senergystring

Prints the calculated energy as a string.

Returns:

  • (string)
    Energy as a string


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/prct06/nutri.rb', line 111

def senergy
	v1 = [@fat, @hydrates, @protein, @salt]
	v2 = [37, 17, 17, 25]
	v3 = [9, 4, 4, 6]

	s1 = 0
	s2 = 0
	i = 0
	while (i < v1.length)
		s1 += v1[i] * v2[i]
		s2 += v1[i] * v3[i]
		i += 1
	end
	@energy = s2
	return "#{s1.round(2)} kJ/g / #{s2.round(2)} kcal/g\n"
end

#sfatstring

Prints fat as a string.

Returns:

  • (string)
    fat as a string


55
56
57
# File 'lib/prct06/nutri.rb', line 55

def sfat
	"#{@fat}"
end

#shydratesstring

Prints hydrates as a string.

Returns:

  • (string)
    hydrates as a string


71
72
73
# File 'lib/prct06/nutri.rb', line 71

def shydrates
	"#{@hydrates}"
end

#snamestring

Prints name as a string.

Returns:

  • (string)
    name as a string


47
48
49
# File 'lib/prct06/nutri.rb', line 47

def sname
	"#{@name}"
end

#sproteinstring

Prints protein as a string.

Returns:

  • (string)
    protein as a string


87
88
89
# File 'lib/prct06/nutri.rb', line 87

def sprotein
	"#{@protein}"
end

#ssaltstring

Prints salt as a string.

Returns:

  • (string)
    salt as a string


95
96
97
# File 'lib/prct06/nutri.rb', line 95

def ssalt
	"#{@salt}"
end

#ssfatsstring

Prints sfats as a string.

Returns:

  • (string)
    sfats as a string


63
64
65
# File 'lib/prct06/nutri.rb', line 63

def ssfats
	"#{@sfats}"
end

#ssugarstring

Prints sugar as a string.

Returns:

  • (string)
    sugar as a string


79
80
81
# File 'lib/prct06/nutri.rb', line 79

def ssugar
	"#{@sugar}"
end

#to_sstring

Prints the whole etiquetta as a string.

Returns:

  • (string)
    Etiquetta as a string


103
104
105
# File 'lib/prct06/nutri.rb', line 103

def to_s
	return "Name: \t\t#{@name}\nfat: \t\t#{@fat}\nsat.fats: \t#{@sfats}\nhydrates: \t#{@hydrates}\nsugar: \t\t#{@sugar}\nprotein: \t#{@protein}\nsalt: \t\t#{@salt}\n"
end