0

私が持っているハッシュは次のとおりです。

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." }]

これを繰り返して、成分だけを取得するにはどうすればよいですか (名前、ガラス、カテゴリなどを返すことなく)。金額にも同じ反復が必要ですが、成分の反復と同じように見えると思います。ばかげた質問で申し訳ありませんが、私はルビーが初めてで、これを何時間も試みました。

4

3 に答える 3

0
>aoh.collect { |i| i[:ingredients].collect { |g| puts g[:ingredient] } }
   Gin
   Vodka
   Lillet Blonde
   White rum
   Lime juice
   Syrup
于 2020-05-07T17:05:29.870 に答える
-1

私は次のことを提案します。

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"]} 
于 2020-05-07T23:52:26.897 に答える