Ruby getter recursively builds tree This is a getter in a class.  It recursively generates a tree/hash to a certain level of depth.

def children(depth: 4, article_children: self.root.child_links)
  get_children = lambda do |depth, article_children|
    article_children.map do |uri|
      if (depth == 0)
        Article.new(uri: uri)
      else
        article = Article.new(uri: uri)
        { article =&gt; get_children.call(depth - 1, article.child_links) }
      end
    end
  end

  @children ||= get_children.call(depth, self.root.child_links)
  return @children
end


I would like feedback on these specific areas:


Readability/maintainability concerns
Is this an appropriate use of lambda?
Style concerns
General feedback
Obviously people are going to have different opinions. I think that adding a lambda just makes it more difficult to read, I would put that in a private emethod (which also has the advantage of making it testable)

There is a bug in your code:

def children(depth: 4, article_children: self.root.child_links)
  @children ||= get_children.call(depth, self.root.child_links)
  return @children
end


You pass self.root_child_links to get_children. You should be passing article_children.

Some style notes:

def children(depth: 4, article_children: self.root.child_links)
  ...    
  @children ||= get_children.call(depth, self.root.child_links)
  return @children
end


The return is generally considered bad style. It is enough to specify just the return value on the last line. i.e. just @children. In this case even that is unnecessary as the assignment above returns the value of @children. iow I would just write:

def children(depth: 4, article_children: self.root.child_links)
  ...    
  @children ||= get_children.call(depth, self.root.child_links)
end


Also (simplifying this piece of code to show my point)

article_children.map do |uri|
  article = Article.new(uri: uri)
  { article =&gt; get_children.call(depth - 1, article.child_links) }
end


This returns an array of hashes which is an odd way to structure data, wouldn't it make sense to return a single hash.

Lastly instead of passing article_links into the method I would pass the article.

My take on this code would be something like:

def children(depth: 4, root: self.root)
  @children ||= recurse_children(depth, root)
end

private

def recurse_children(depth, article)
  article.child_links.reduce do |hash, uri|
    article = Article.new(uri: uri)
    hash[article] = depth &gt; 0 ? recurse_children(article) : nil
    hash
  end
end