We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent db48c35 commit a6f966fCopy full SHA for a6f966f
1 file changed
lib/tree.rb
@@ -767,6 +767,28 @@ def each_leaf &block
767
end
768
769
770
+ # Yields every level of the (sub)tree rooted at this node to the
771
+ # specified block.
772
+ #
773
+ # Will yield this node as well since it is considered the first level.
774
775
+ # @yieldparam level [Array<Tree::TreeNode>] All nodes in the level
776
777
+ # @return [Tree::TreeNode] this node, if a block if given
778
+ # @return [Enumerator] an enumerator on this tree, if a block is *not* given
779
+ def each_level &block
780
+ if block_given?
781
+ level = [self]
782
+ until level.empty?
783
+ yield level
784
+ level = level.map(&:children).flatten
785
+ end
786
+ return self
787
+ else
788
+ self.each
789
790
791
+
792
# @!endgroup
793
794
# @!group Navigating the Child Nodes
0 commit comments