递归树构建函数仅映射一个孩子(Recursive tree-building function only maps over one child)
我试图递归构建一棵树。 下面的类通过递归调用
.children
getter将树构建到Hash中。class Tree attr_reader :root def initialize(uri:) @root = Article.new(uri: uri) @children = nil end def children(depth: 3, article_children: self.root.child_links) @children ||= article_children.map do |uri| puts "URI: #{uri}" if (depth == 0) return Article.new(uri: uri) else article = Article.new(uri: uri) return { article => self.children(depth: depth - 1, article_children: article.child_links) } end end puts JSON.pretty_generate(@children) return @children end end
我期望得到的JSON看起来像这样:
{ "#<Article:0x00007ff1a33c06d0>": { "#<Article:0x00007ff1a345e920>": { "#<Article:0x00007ff1a3989418>": ["#<Article:0x00007ff1a423f698>", ...] }, "#<Article:0x00007ff1a345e920>": { "#<Article:0x00007ff1a3989418>": ["#<Article:0x00007ff1a423f698>", ...] }, "#<Article:0x00007ff1a345e920>": { "#<Article:0x00007ff1a3989418>": ["#<Article:0x00007ff1a423f698>", ...] }, "#<Article:0x00007ff1a345e920>": { "#<Article:0x00007ff1a3989418>": ["#<Article:0x00007ff1a423f698>", ...] }, "#<Article:0x00007ff1a345e920>": { "#<Article:0x00007ff1a3989418>": ["#<Article:0x00007ff1a423f698>", ...] }, } } }
然而,美化的JSON最终看起来像这样:
{ "#<Article:0x00007ff1a33c06d0>": { "#<Article:0x00007ff1a345e920>": { "#<Article:0x00007ff1a3989418>": "#<Article:0x00007ff1a423f698>" } } }
并在
article.children.map
输出中puts "URI: #{uri}
URI: https://child.uri.org/example/uri URI: https://child.uri.org/example/uri1 URI: https://child.uri.org/example/uri2 URI: https://child.uri.org/example/uri3
每个URI都是每个项目对象的第一个子URI。
为什么map仅处理第一个元素,而不处理整个数组?
I'm attempting to recursively build a tree. The following class builds the tree into a Hash by recursively calling the
.children
getter.class Tree attr_reader :root def initialize(uri:) @root = Article.new(uri: uri) @children = nil end def children(depth: 3, article_children: self.root.child_links) @children ||= article_children.map do |uri| puts "URI: #{uri}" if (depth == 0) return Article.new(uri: uri) else article = Article.new(uri: uri) return { article => self.children(depth: depth - 1, article_children: article.child_links) } end end puts JSON.pretty_generate(@children) return @children end end
I expect the resulting JSON to look something like this:
{ "#<Article:0x00007ff1a33c06d0>": { "#<Article:0x00007ff1a345e920>": { "#<Article:0x00007ff1a3989418>": ["#<Article:0x00007ff1a423f698>", ...] }, "#<Article:0x00007ff1a345e920>": { "#<Article:0x00007ff1a3989418>": ["#<Article:0x00007ff1a423f698>", ...] }, "#<Article:0x00007ff1a345e920>": { "#<Article:0x00007ff1a3989418>": ["#<Article:0x00007ff1a423f698>", ...] }, "#<Article:0x00007ff1a345e920>": { "#<Article:0x00007ff1a3989418>": ["#<Article:0x00007ff1a423f698>", ...] }, "#<Article:0x00007ff1a345e920>": { "#<Article:0x00007ff1a3989418>": ["#<Article:0x00007ff1a423f698>", ...] }, } } }
However, the prettified JSON ends up looking like this:
{ "#<Article:0x00007ff1a33c06d0>": { "#<Article:0x00007ff1a345e920>": { "#<Article:0x00007ff1a3989418>": "#<Article:0x00007ff1a423f698>" } } }
And
puts "URI: #{uri}
withinarticle.children.map
outputsURI: https://child.uri.org/example/uri URI: https://child.uri.org/example/uri1 URI: https://child.uri.org/example/uri2 URI: https://child.uri.org/example/uri3
Each of these URIs is the first child URI of each article object.
Why is map only processing the first element, and not the entirety of the array?
原文:https://stackoverflow.com/questions/49787048