Hello, I’ve been working on this project quite a bit. I’m very
enthusiastic about learning Ruby, and have done my best to comment with
MARKERs where the problems are or may be. The hurdle is that I need
output to print values for each listed unit of measure per food item, in
this case salted butter. The units are cup, tbsp, pat, and stick. I
can only seem to get the printouts for cup or stick. Please review my
code and help me fix it, or suggest more efficient alternatives. Thank
you kindly.
def nutrients(food_id)
@food = FoodDescription.find(food_id)
@nutrients = []
@nutrients = NutrientDatum.find_all_by_ndb_id(@food.ndb_id)
@n = 0
@definition = []
@values = []
@nutrient_id = @nutrients[@n].nutrient_id
@nutrient_description =
NutrientDefinition.find_by_nutrient_id(@nutrient_id).description
@definition[@n] = @nutrient_description
# TODO calculate for units serving
@values[@n] = @nutrients[@n].nutrient_value
@units = units(@food.ndb_id)
@m = 1
@composite = []
until @m > @units.length
@composite[@m - 1] = calculate(@food, @nutrients, @m)
@m += 1
end
# MARKER 1 # print @out returns @composite[0 through 8]
# but only for the last unit, i.e. cup(1),
# tbsp(2), pat(3), => stick(4) of butter...
# I don't know why it's not printing 4 arrays,
# eight hashes in each array
@out = @composite
#system("echo #{@out}")
print @out
end
def units(ndb_id)
@units = []
@units = FoodWeight.find_all_by_ndb_id(ndb_id)
@units
end
def calculate(food, nutes, unit)
@n = 0
calories = {} # MARKER 2 # may be a problem here
energy = {}
protein = {}
fats = {}
carbs = {}
vitamins = {}
minerals = {}
sterols = {}
other = {}
@composite = []
until @n == nutes.length
info = FoodWeight.find_by_ndb_id_and_sequence_id(food.ndb_id,
unit)
results = {}
results[:amount] = info.amount == 1.0 ?
results[:amount] = 1 : results[:amount] =
info.amount
results[:description] = info.unit_description
results[:grams] = info.gram_weight
# TODO get nutrient weights per unit weight
@description = NutrientDefinition.find(@n + 1).description
@grams_unit = (nutes[@n].nutrient_value / 100 *
results[:grams]).to_i
@key = " #{results[:amount]} #{results[:description]}
#{@grams_unit} \n"
case @description
# Main Totals
when "Protein" then protein[:total_protein] = @key
when "Total lipid (fat)" then fats[:total_fat] = @key
when "Carbohydrate, by difference" then carbs[:total_carbs] =
@key
when “Energy” then
if @description == “kcal”
calories[:energy_kcal] = @key
else
calories[:energy_kj] = @key
end
# Carbs
when "Starch" then carbs[:starch] = @key
.
.
.
# Minerals
when "Calcium, Ca" then minerals[:calcium] = @key
.
.
.
# Vitamins
when "Manganese, Mn" then vitamins[:manganese] = @key
.
.
.
# Protein and amino acids
.
.
.
# Fats
.
.
.
# Sterols
.
.
.
# Other
.
.
.
when "Theobromine" then other[:theobromine] = @key
end
@n += 1
end
# TODO calculate energy
@composite[0] = calories
@composite[1] = energy
@composite[2] = protein
@composite[3] = fats
@composite[4] = carbs
@composite[5] = vitamins
@composite[6] = minerals
@composite[7] = sterols
@composite[8] = other
@composite
end
== Sample output
…
sodium 1 stick 772
calcium 1 stick 3
zinc 1 stick 178
iron 1 stick 0
copper 1 stick 0
magnesium 1 stick 1
fluoride 1 stick 2
phosphorus 1 stick 2823
cholesterol 1 stick 11
phytosterols 1 stick 0
caffeine 1 stick 2
theobromine 1 stick 27
…