Top Level Namespace

Defined Under Namespace

Modules: DLLModule, FoodGem Classes: Food, FoodAbstract

Constant Summary

PROTEIN_ENERGY =

Constants of energy

4
GLUCID_ENERGY =
4
LIPID_ENERGY =
9

Instance Method Summary collapse

Instance Method Details

#read_data(data_filename) ⇒ Array

Method to read data by file

Returns:

  • (Array)

    Return array of food



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/food/food_class.rb', line 99

def read_data (data_filename)
data_string = File.open(data_filename).read.split("\n") # Divido el fichero en string de lineas
food_array = []

data_string.each { |data_line|
  data_line = data_line.split(" ") # La divido en espacios
  name = ""
  
  while (data_line[0] != data_line[0].to_f.to_s) # Si el nombre no cambia al pasar de string afloat es que es un float
    name << data_line[0] << " "
    data_line = data_line[1..-1] # Quito el primer elemento
  end
  
  protein = [data_line[0].to_f, PROTEIN_ENERGY]
  glucid = [data_line[1].to_f, GLUCID_ENERGY]
  lipid = [data_line[2].to_f, LIPID_ENERGY]
  
  data_line = data_line[3..-1] 
  
  group_name = ""
  while (!data_line[0].nil?) # Si el nombre no cambia al pasar de string afloat es que es un float
    group_name << data_line[0] << " "
    data_line = data_line[1..-1] # Quito el primer elemento
  end
  
  food_array.push(Food.new(name[0..-2], protein, glucid, lipid, group_name[0..-2])) # Quito Ășltimo espacio a nombre y grupo
}

return food_array
end