Sha256: 6390d1ee02b53c4dfd79632d7e28cd433a5f1729f585731894f9e4e79fd69883

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

class Recipe
  attr_accessor :name, :ingredients, :instructions

  def initialize(name, &block)
    self.name = name
    self.ingredients = []
    self.instructions = []

    if block_given?  
      if block.arity == 1
        yield self
      else
       instance_eval &block 
      end
    end
  end
  
  def to_s
    output = name
    output << "\n#{'=' * name.size}\n\n"
    output << "Ingredients: #{ingredients.join(', ')}\n\n"

    instructions.each_with_index do |instruction, index|
      output << "#{index + 1}) #{instruction}\n"
    end

    output
  end

  def ingredient(name, options = {})
    ingredient = name
    ingredient << " (#{options[:amount]})" if options[:amount]

    ingredients << ingredient
  end

  def step(text, options = {})
    instruction = text
    instruction << " (#{options[:during]})" if options[:during]

    instructions << instruction
  end
end

mac_and_cheese = Recipe.new("Noodles and Cheese") do 
  ingredient "Water",   :amount => "2 cups"
  ingredient "Noodles", :amount => "1 cup"
  ingredient "Cheese",  :amount => "1/2 cup"

  step "Heat water to boiling.",        :during => "5 minutes"
  step "Add noodles to boiling water.", :during => "6 minutes"
  step "Drain water."
  step "Mix cheese in with noodles."
end

puts mac_and_cheese

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
Dieta.alu0100454437-1.1.6 lib/practica8/recipe.rb
Dieta.alu0100454437-0.1.6 lib/practica8/recipe.rb
Bibliografia.alu0100816167-0.2.0 lib/Bibliografia/recipe.rb