如何在 Ruby 中遍历 YAML 树中的所有节点?

发布于 2024-08-01 22:12:30 字数 208 浏览 7 评论 0原文

我正在加载任意 YAML 文档,并且想要遍历树中的每个节点。 我事先不知道树是如何嵌套的,所以我不能只使用简单的each语句来遍历所有节点。

这是我加载文档的方式:

tree = File.open( "#{RAILS_ROOT}/config/locales/es.yml" ){ |yf| YAML::load (yf)}

I am loading an arbitrary YAML document, and want to walk every node in the tree. I don't know how nested the tree is beforehand, so I can't just use a simple each statement to walk all the nodes.

Here is how I'm loading the document:

tree = File.open( "#{RAILS_ROOT}/config/locales/es.yml" ){ |yf| YAML::load (yf)}

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

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

发布评论

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

评论(2

债姬 2024-08-08 22:12:30

以下是如何解析 YAML 树结构并作用于每个节点、分支和叶子。 它还为您提供所需的每个节点的父节点,例如,从 YAML 文件填充数据库树结构时。 这是对 @sepp2k 优秀答案的一个小小的补充:

require 'yaml'

def traverse(obj,parent, &blk)
 case obj
 when Hash
   obj.each do |k,v| 
     blk.call(k,parent)
     # Pass hash key as parent
     traverse(v,k, &blk) 
   end
 when Array
   obj.each {|v| traverse(v, parent, &blk) }
 else
   blk.call(obj,parent)
 end
end


# Example, creating a database tree structure, from your YAML file.
# Passing nil to root node(s) as parent

tree_str =<<EOF
  Regions:
    - Asia
    - Europe
    - Americas
  Other:
    - Foo:
      - bar
      - buz
EOF

traverse( YAML.load(tree_str), nil ) do |node,parent|
  puts "Creating node '#{node}' with parent '#{ parent || 'nil' }'"
end

Here is how to parse a YAML tree structure and to act on each node, branch and leaf. It also gives you parent of each node that you need, for example, when populating a database tree structure from your YAML file. It is a small addition to @sepp2k excellent answer:

require 'yaml'

def traverse(obj,parent, &blk)
 case obj
 when Hash
   obj.each do |k,v| 
     blk.call(k,parent)
     # Pass hash key as parent
     traverse(v,k, &blk) 
   end
 when Array
   obj.each {|v| traverse(v, parent, &blk) }
 else
   blk.call(obj,parent)
 end
end


# Example, creating a database tree structure, from your YAML file.
# Passing nil to root node(s) as parent

tree_str =<<EOF
  Regions:
    - Asia
    - Europe
    - Americas
  Other:
    - Foo:
      - bar
      - buz
EOF

traverse( YAML.load(tree_str), nil ) do |node,parent|
  puts "Creating node '#{node}' with parent '#{ parent || 'nil' }'"
end
时间你老了 2024-08-08 22:12:30
def traverse(obj, &blk)
  case obj
  when Hash
    # Forget keys because I don't know what to do with them
    obj.each {|k,v| traverse(v, &blk) }
  when Array
    obj.each {|v| traverse(v, &blk) }
  else
    blk.call(obj)
  end
end

traverse( YAML.load_file(filename) ) do |node|
  puts node
end

编辑:

请注意,这只会产生叶节点。 这个问题不太清楚到底想要什么。

def traverse(obj, &blk)
  case obj
  when Hash
    # Forget keys because I don't know what to do with them
    obj.each {|k,v| traverse(v, &blk) }
  when Array
    obj.each {|v| traverse(v, &blk) }
  else
    blk.call(obj)
  end
end

traverse( YAML.load_file(filename) ) do |node|
  puts node
end

Edit:

Note that this only yields the leaf nodes. The question wasn't very clear as to what was wanted exactly.

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