Skip to content

Instantly share code, notes, and snippets.

@poteto
Created September 18, 2012 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poteto/3743679 to your computer and use it in GitHub Desktop.
Save poteto/3743679 to your computer and use it in GitHub Desktop.
Cooking for Programmers: Spaghetti Carbonara
class Recipe
attr_accessor :name, :servings, :ingredients, :instructions
def initialize(name, servings=1, &block)
self.name = name
self.servings = servings
self.ingredients = []
self.instructions = []
instance_eval &block
end
def ingredient(name, options = {})
ingredient = {"name" => name, "amount" => options[:amount], "unit" => options[:unit]}
ingredients << ingredient
end
def step(text, options = {})
instruction = text
instruction << " (#{options[:for]})" if options[:for]
instructions << instruction
end
def to_s
puts "\n#{name} recipe"
output = ""
output << "#{'=' * (name.size + 10)}\n\n"
output << "Servings:\n #{servings}\n\n"
output << "Ingredients:\n"
ingredients.each do |ingredient|
output << " #{ingredient['name']} (#{ingredient['amount'] * servings} #{ingredient['unit']})\n"
end
output << "\n"
output << "Instructions:\n"
instructions.each_with_index do |instructions, index|
output << " #{index + 1}) #{instructions}\n"
end
output
end
spaghetti_carbonara = Recipe.new("Spaghetti Carbonara", 2) do
ingredient "Spaghetti", :amount => 125, :unit => "g"
ingredient "Unsalted butter", :amount => 2, :unit => "tbsp"
ingredient "Shallot", :amount => 0.5, :unit => "pieces"
ingredient "Garlic", :amount => 1, :unit => "cloves"
ingredient "Mushrooms", :amount => 75, :unit => "g"
ingredient "Pancetta", :amount => 3, :unit => "rashers"
ingredient "Egg", :amount => 1, :unit => "egg"
ingredient "Double cream", :amount => 75, :unit => "ml"
ingredient "Parmesan", :amount => 15, :unit => "g"
ingredient "Thyme", :amount => 1, :unit => "sprig"
ingredient "Sea salt", :amount => 1, :unit => "pinch"
ingredient "Black pepper", :amount => 1, :unit => "pinch"
step "Prepare ingredients:\nFinely chop shallot and garlic, then cut the pancetta and mushrooms into thin strips. Grate your parmesan if it isn't already grated."
step "Cook the pasta:\nBring a large pot of water to the boil, then add a pinch of salt but no oil. Once boiling, add the spaghetti and cook until al dente (about 1 minute under the recommended time on the packet). Proceed with the next steps while waiting."
step "Prepare sauce:\nCrack eggs into a bowl and beat together with the cream, most of the parmesan, sea salt and ground black pepper. Set aside."
step "Cook the components:\nAdd the pancetta to a very hot skillet, then throw in the chopped garlic and shallot together with the butter. Fry for a few minutes and then add the mushrooms. When golden, take off the heat."
step "Combine:\nUsing a pair of tongs, place the cooked spaghetti into the skillet. Pour in the sauce from Step 3, and mix together. Add a spoonful or 2 of the water you cooked the pasta in to moisten the pasta. Throw in the rest of the parmesan, and add salt or pepper to taste. If the pasta gets dry, add more pasta water."
step "Eat."
end
puts spaghetti_carbonara
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment