解析嵌套 Ruby 数组/YAML 的最快方法

发布于 2024-10-12 13:58:42 字数 1367 浏览 4 评论 0原文

如果它不对称,从该结构中提取第三级元素的最简洁方法是什么:

   yaml = [
    {"Collection"=>[
       {"Jeans"=>[
          {"Trends"=>[
             {"Building on basics"=>"Jeans"},
             {"Unexpected pairings"=>"Jeans"},
             {"Retro chic"=>"Jeans"}    # extract me
          ]},
          {"Styles"=>[
             {"Straight Leg"=>"Straight Leg"},
             {"Bootcut"=>"Bootcut"},
             {"Trouser"=>"Trouser"},
             {"Slim Leg"=>"Slim Leg"}
          ]},
          {"Wash"=>[
             {"Blues"=>"Blues"},
             {"Black/Grey"=>"BlackGrey"},
             {"Whites"=>"Summer Whites"},
             {"Colors"=>"Colors"},
             {"Resin"=>"Resin"}
          ]},
          {"Details"=>[
             {"Embroidered"=>"Embroidered"},
             {"Tuxedo"=>"Tuxedo"},
             {"Jeweled"=>"Jeweled"}
          ]}
       ]},
       {"Bodyshapers"=>[
          {"All"=>[
             {"All"=>"BodyShapers"}
          ]}
       ]}
    ]},
    {"Lift Tuck"=>nil},
    {"Find Us" =>[
       "By City",
       "International Websites",
       "Online Retailers"
    ]},
    {"Your Stories"=>nil},
    {"The Skinny"=>[
       "Trends",
       "Behind the Scenes",
       "VIP Events",
       "In the News",
       "Win a Pair"
    ]}
  ]

What's the cleanest way to extract third level elements from this structure provided that it's not symmetrical:

   yaml = [
    {"Collection"=>[
       {"Jeans"=>[
          {"Trends"=>[
             {"Building on basics"=>"Jeans"},
             {"Unexpected pairings"=>"Jeans"},
             {"Retro chic"=>"Jeans"}    # extract me
          ]},
          {"Styles"=>[
             {"Straight Leg"=>"Straight Leg"},
             {"Bootcut"=>"Bootcut"},
             {"Trouser"=>"Trouser"},
             {"Slim Leg"=>"Slim Leg"}
          ]},
          {"Wash"=>[
             {"Blues"=>"Blues"},
             {"Black/Grey"=>"BlackGrey"},
             {"Whites"=>"Summer Whites"},
             {"Colors"=>"Colors"},
             {"Resin"=>"Resin"}
          ]},
          {"Details"=>[
             {"Embroidered"=>"Embroidered"},
             {"Tuxedo"=>"Tuxedo"},
             {"Jeweled"=>"Jeweled"}
          ]}
       ]},
       {"Bodyshapers"=>[
          {"All"=>[
             {"All"=>"BodyShapers"}
          ]}
       ]}
    ]},
    {"Lift Tuck"=>nil},
    {"Find Us" =>[
       "By City",
       "International Websites",
       "Online Retailers"
    ]},
    {"Your Stories"=>nil},
    {"The Skinny"=>[
       "Trends",
       "Behind the Scenes",
       "VIP Events",
       "In the News",
       "Win a Pair"
    ]}
  ]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍暮颜 2024-10-19 13:58:42

我不知道最简单的方法,但这样的方法会起作用:

class NthElements
  include Enumerable

  def initialize(yaml, n = 3)
    @yaml = yaml
    @n = n
  end

  def each(&block)
    traverse(@yaml, 0, &block)
  end

  private
  def traverse(value, level, &block)
    if level == @n + 1
      block.call(value)
      return
    end

    case value
    when Array
      value.each { |next_value| traverse(next_value, level + 1, &block) }
    when Hash
      value.values.each { |next_value| traverse(next_value, level, &block) }
    end
  end
end

NthElements.new(yaml).each{|val| p val }

I don't know about the easiest way, but something like this would work:

class NthElements
  include Enumerable

  def initialize(yaml, n = 3)
    @yaml = yaml
    @n = n
  end

  def each(&block)
    traverse(@yaml, 0, &block)
  end

  private
  def traverse(value, level, &block)
    if level == @n + 1
      block.call(value)
      return
    end

    case value
    when Array
      value.each { |next_value| traverse(next_value, level + 1, &block) }
    when Hash
      value.values.each { |next_value| traverse(next_value, level, &block) }
    end
  end
end

NthElements.new(yaml).each{|val| p val }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文