class HarvardDish

Public Class Methods

new(name) { |self| ... } click to toggle source
# File lib/nutrientes/harvard_dish.rb, line 41
def initialize(name, &block)
    @name = name
    @ingredients = []
    @amounts = []
    @cv_total = 0
    
    if block_given?
        if block.arity == 1 then
            yield self
        else
            instance_eval(&block)
        end
    end
end

Public Instance Methods

ingredient(nombre, cantidad) click to toggle source
# File lib/nutrientes/harvard_dish.rb, line 68
def ingredient(nombre, cantidad)
    @@list.each_index do |i|
        if @@list[i].name == nombre then
            @ingredients << @@list[i]
            numero = /\d/.match(cantidad)
            multiplier = 0
            @@proportions.each_index do |j|
                expr = Regexp.new @@proportions[j][0]
                if expr.match(cantidad) != nil then
                    multiplier = @@proportions[j][1]
                    break;
                end
            end
            multiplier += numero[0].to_i
            @amounts << multiplier
            break
        end
    end
end
to_s() click to toggle source
# File lib/nutrientes/harvard_dish.rb, line 56
def to_s()
    output = @name
    output << "\n#{'=' * @name.size}\n"
    output << "Nombre  Proteínas  Glúcidos  Lípidos  Valor calórico\n"
    @ingredients.each_index do |i|
        output << @ingredients[i].name << " " << @ingredients[i].proteins.to_s << " " << @ingredients[i].glucids.to_s << " " << @ingredients[i].lipids.to_s << " " << (@ingredients[i].caloric_value * @amounts[i]).to_s << "\n"
        @cv_total += (@ingredients[i].caloric_value * @amounts[i])
    end
    output << "Valor calórico total: " << @cv_total.to_s << "\n"
    return output
end