class Plato attr_accessor :name, :ingredients def initialize(name, &block_ingredientes) @name = name @ingredients = [] if block_given? if block_ingredientes.arity == 1 yield self else instance_eval(&block_ingredientes) end end @tablaAlimentos = [@huevo_frito = Comida.new("Huevo", 14.1, 0.0, 19.5), @leche_vaca = Comida.new("Leche vaca", 3.3, 4.8, 3.2), @Yogurt = Comida.new("Yogurt", 3.8, 4.9, 3.8), @cerdo = Comida.new("Cerdo", 21.5, 0.0, 6.3), @ternera = Comida.new("Ternera", 21.1, 0.0, 3.1), @pollo = Comida.new("Pollo", 20.6, 0.0, 5.6), @bacalao = Comida.new("Bacalao", 17.7, 0.0, 0.4), @atun = Comida.new("Atun", 21.5, 0.0, 15.5), @salmon = Comida.new("Salmon", 19.9, 0.0, 13.6), @aceite_oliva = Comida.new("Aceite de oliva", 0.0, 0.2, 99.6), @chocolate = Comida.new("Chocolate", 5.3, 47.0, 30.0), @azucar = Comida.new("Azucar", 0.0, 99.8, 0.0), @arroz = Comida.new("Arroz", 6.8, 77.7, 0.6), @lentejas = Comida.new("Lentejas", 23.5, 52.0, 1.4), @papas = Comida.new("Papas", 2.0, 15.4, 0.1), @tomate = Comida.new("Tomate", 1.0, 3.5, 0.2), @cebolla = Comida.new("Cebolla", 1.3, 5.8, 0.3), @platanos = Comida.new("Platano", 1.2, 21.4, 0.2), @manzana = Comida.new("Manzana", 0.3, 12.4, 0.4)] @tablaValores = [["2 piezas pequeñas", 20], ["1 taza", 10], ["1/2 cucharon", 7], ["1/2 cucharada", 5], ["1 pieza", 15]] end def to_s output = @name output << "\n#{'=' * @name.size}\n\n" output << "Composicion nutricional:\n" output << "#{' ' * 20}glucidos proteinas lipidos valor energetico\n" total = 0 @ingredients.each do |n| buscador = "#{n.match(/[^.\(]*/)}" output << buscador cantidad = "#{n[/\(.*\)/]}" cantidad[0] = '' #Elimina el parentisis ( cantidad[cantidad.length-1] = '' #Elimina el parentisis de la cantidad ) valor = 0 @tablaValores.collect do |n| n.each_with_index do |m, i| if (cantidad == m.to_s) valor = n[1] end end end @tablaAlimentos.collect do |n| if (n.comida == buscador) total += (n.valorEnergetico * valor) output << "#{' ' * (20-buscador.size)}#{n.glucidos} #{n.proteina} #{n.lipidos} #{n.valorEnergetico*valor}\n" end end end output << "Valor energetico total: #{total}" output end def vegetal(name, options = {}) ingredient = name ingredient << "(#{options[:porcion]})" if options[:porcion] @ingredients << ingredient end def fruta(name, options = {}) ingredient = name ingredient << "(#{options[:gramos]})" if options[:gramos] @ingredients << ingredient end def cereal(name, options = {}) ingredient = name ingredient << "(#{options[:porcion]})" if options[:porcion] @ingredients << ingredient end def proteina(name, options = {}) ingredient = name ingredient << "(#{options[:porcion]})" if options[:porcion] @ingredients << ingredient end def aceite(name, options = {}) ingredient = name ingredient << "(#{options[:porcion]})" if options[:porcion] @ingredients << ingredient end end