-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_recipe_file.rb
44 lines (36 loc) · 1.15 KB
/
read_recipe_file.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require_relative 'recipe_methods.rb'
text = File.read("recipes.txt").split("\n\n")
recipe_array = text.collect do |x|
x.split("\n")
end
recipe_array.each do |recipe|
current_recipe = Recipe.new(recipe[0], recipe[1].split.last.to_i)
n = 2
while n < recipe.size
ingredients_split = recipe[n].split
current_qty = "--" #default value
current_unit = "--"
current_prep = "--"
if ingredients_split[0].match(/\d+/)
current_qty = ingredients_split[0].match(/\d+/).to_a[0].to_i
ingredients_split.shift
end
array_of_units = ["oz", "tbs", "lbs", "cup", "cups"]
if array_of_units.include?(ingredients_split[0])
current_unit = ingredients_split[0]
ingredients_split.shift
end
i = ingredients_split.find_index { |x| x.end_with?(",")}
if i != nil
x = ingredients_split.take(i+1)
ingredients_split.shift(i+1)
current_prep = ingredients_split.join(" ")
ingredients_split = x
ingredients_split[-1] = ingredients_split.last.chop
end
current_ingredient = Ingredient.new(current_qty, current_unit, ingredients_split.join(" "), current_prep)
current_recipe.add_ingredient(current_ingredient)
n += 1
end
p current_recipe
end