title: How to iterate over just one key in an array of hashes in Ruby?
body: <p>Complete ruby noob trying to build projects to practice. My aim here is to create a recipe converter which can take user input to form an array of hashes, and then converts the portion size of the recipe.</p>
  <p>Each hash is one ingredient with the following keys: quantity, metric, ingredient (so: 500 g butter =&gt; {quantity: 500, metric: g, ingredient: butter}). This is so I can manipulate the various parts of the ingredients eg. convert portion sizes, or oz to g (later). This example deals with the easiest manipulation - portion size i.e changing recipe quantities.</p>
  <p>Here's the code which takes user input and generates the array of hashes:</p>
  <pre><code>
  puts &quot;Please enter your recipe. Press X when done.&quot; 
  puts &quot;Enter ingredient&quot;
  
  recipe = []
  ingredient = &quot;default&quot;
  metric = &quot;g&quot;
  quantity = 1
  
  def create_ingredient(quantity, metric, ingredient)
      {
          quantity: quantity,
          metric: metric,
          ingredient: ingredient
      }
  end
  
  loop do 
      ingredient = gets.chomp.capitalize 
          if ingredient == &quot;X&quot;
          break loop
          end
      puts &quot;Enter metric&quot;
          metric = gets.chomp.downcase
      puts &quot;Enter quantity&quot;
          quantity = gets.chomp
  
      recipe.push(create_ingredient(quantity, metric, ingredient))    
      puts &quot;Enter another ingredient, if done press X&quot;
  
  end
  
  puts recipe
          
  </code></pre>
  <p>I kept getting name errors until I put the default values at the top but this part works. (any suggestions for cleaning up welcome!)</p>
  <p>This is the converter:</p>
  <pre><code>
  #conversion code to change portion size
  puts &quot;How many portions is this recipe for?&quot;
      orig_portion = gets.chomp.to_i
      Integer(orig_portion)
      unless (orig_portion.is_a?(Integer) == true)
          puts &quot;Please enter a number&quot;
          orig_portion = gets.chomp
      end
  puts &quot;How many people are you cooking for?&quot;
      new_portion = gets.chomp.to_i
      Integer(new_portion)
      unless (new_portion.is_a?(Integer) == true)
          puts &quot;Please enter a number&quot;
          new_portion = gets.chomp
      end
  
  #not working from here 
  
  def convert_recipe(recipe)
      recipe.each do |key, value|
          puts &quot;#{( ( value recipe[&quot;quantity&quot;.to_i] / orig_portion ) *new_portion )} #{recipe[&quot;metric&quot;]} #{recipe[&quot;ingredient&quot;]}&quot;
      end
  end
  
  puts convert_recipe(recipe)
  </code></pre>
  <p>Right now, the code runs and generates the array of hashes, takes the portion input but then gives this error:</p>
  <pre><code>Traceback (most recent call last):
          3: from recipe calc2.rb:66:in `&lt;main&gt;'
          2: from recipe calc2.rb:61:in `convert_recipe'
          1: from recipe calc2.rb:61:in `each'
  recipe calc2.rb:62:in `block in convert_recipe': undefined local variable or method `orig_portion' for main:Object (NameError)
  </code></pre>
  <p>I've tried a few suggestions from other stackoverflow answers on iterating over an array of hashes, some of which are incorporated here. I tried using <code>(quantity, **)</code> as the argument for the <code>convert_recipe</code> method but it raised a name error on the method.</p>
  <p>I can't understand why I keep getting name errors when I've defined the variables or methods (as far as I can tell) so any help understanding these errors would be so useful.</p>
  <p>Thanks a lot for any help or suggestions!</p>