0

the hash I have is the following:

aoh=[
  { "name": "Vesper",
    "glass": "martini",
    "category": "Before Dinner Cocktail",
    "ingredients": [
      { "unit": "cl",
        "amount": 6,
        "ingredient": "Gin" },
      { "unit": "cl",
        "amount": 1.5,
        "ingredient": "Vodka" },
      { "unit": "cl",
        "amount": 0.75,
        "ingredient": "Lillet Blonde" }
    ],
    "garnish": "Lemon twist",
    "preparation": "Shake and strain into a chilled cocktail glass." },
  { "name": "Bacardi",
    "glass": "martini",
    "category": "Before Dinner Cocktail",
    "ingredients": [
      { "unit": "cl",
        "amount": 4.5,
        "ingredient": "White rum",
        "label": "Bacardi White Rum" },
      { "unit": "cl",
        "amount": 2,
        "ingredient": "Lime juice" },
      { "unit": "cl",
        "amount": 1,
        "ingredient": "Syrup",
        "label": "Grenadine" }
    ],
    "preparation": "Shake with ice cubes. Strain into chilled cocktail glass." }]

How can I iterate through this to get JUST the ingredient (without returning name,glass,category,etc.)? I also need the same iteration for amount but I assume that will look just like the iteration for ingredient. Sorry for the dumb question, I'm new to ruby and have attempted this for hours now.

3 Answers 3

0

You have an array of two elements in your example. Those two elements are hashes with key/value pairs. You can loop through the array with the #each method and access the values that the :"ingredients" keys store like this:

aoh.each do |hash|
  hash[:ingredients]
end

The :ingredients keys each store another array of hashes. An example hash is:

{ "unit": "cl",
        "amount": 6,
        "ingredient": "Gin" }

You can then access the value under the :ingredient key by doing hash[:ingredient]. The final result looks something like this:

   aoh.each do |array_element|
    array_element[:ingredients].each do |ingredient|
      ingredient[:ingredient]
    end
  end

This currently only iterates through the arrays and hashes. If you want to also print the result you can do this:

  aoh.each do |array_element|
    array_element[:ingredients].each do |ingredient|
      puts ingredient[:ingredient]
    end
  end
#=> Gin
#   Vodka
#   Lillet Blonde
#   White rum
#   Lime juice
#   Syrup

If you want to get a modified array, you can use #map (or #flat_map). You can also get the amount with the value like this:

   aoh.flat_map do |array_element|
    array_element[:ingredients].map do |ingredient|
      [[ingredient[:ingredient], ingredient[:amount]]
    end
  end
#=> [["Gin", 6], ["Vodka", 1.5], ["Lillet Blonde", 0.75], ["White rum", 4.5], ["Lime juice", 2], ["Syrup", 1]]
1
  • Thanks Viktor, very helpful!
    – st123
    May 8, 2020 at 13:46
0
>aoh.collect { |i| i[:ingredients].collect { |g| puts g[:ingredient] } }
   Gin
   Vodka
   Lillet Blonde
   White rum
   Lime juice
   Syrup
1
  • While it’s acceptable to provide code-only answers, it’s often more useful for the community if you can also provide an explanation of the code and help people understand why it addresses the problem. That can reduce the number of follow-up questions, and help new developers understand the underlying concepts. Would you mind updating your question with additional detail? May 10, 2020 at 0:28
-1

I would suggest the following.

aoh=[
     { "name": "Vesper",
       "ingredients": [
         { "unit": "cl", "ingredient": "Gin" },
         { "unit": "cl", "ingredient": "Vodka" }
       ],
       "garnish": "Lemon twist"
     },
     { "name": "Bacardi",
       "ingredients": [
         { "unit": "cl", "ingredient": "White rum" },
         { "unit": "cl", "ingredient": "Lime juice" }
       ],
     }
   ]

aoh.each_with_object({}) { |g,h| h[g[:name]] =
  g[:ingredients].map { |f| f[:ingredient] } }
  #=> {"Vesper"=>["Gin", "Vodka"], "Bacardi"=>["White rum", "Lime juice"]} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.