@@ -130,38 +130,44 @@ class TreeNode
130130 # @return [Tree::TreeNode] Root of the (sub)tree.
131131 def root
132132 root = self
133- root = root . parent until root . is_root ?
133+ root = root . parent until root . root ?
134134 root
135135 end
136136
137- # @!attribute [r] is_root ?
137+ # @!attribute [r] root ?
138138 # Returns +true+ if this is a root node. Note that
139139 # orphaned children will also be reported as root nodes.
140140 #
141141 # @return [Boolean] +true+ if this is a root node.
142- def is_root ?
142+ def root ?
143143 @parent . nil?
144144 end
145145
146- # @!attribute [r] has_content?
146+ alias is_root? root? # @todo: Aliased for eventual replacement
147+
148+ # @!attribute [r] content?
147149 # +true+ if this node has content.
148150 #
149151 # @return [Boolean] +true+ if the node has content.
150- def has_content ?
152+ def content ?
151153 @content != nil
152154 end
153155
154- # @!attribute [r] is_leaf?
156+ alias has_content? content? # @todo: Aliased for eventual replacement
157+
158+ # @!attribute [r] leaf?
155159 # +true+ if this node is a _leaf_ - i.e., one without
156160 # any children.
157161 #
158162 # @return [Boolean] +true+ if this is a leaf node.
159163 #
160- # @see #has_children ?
161- def is_leaf ?
162- !has_children ?
164+ # @see #children ?
165+ def leaf ?
166+ !children ?
163167 end
164168
169+ alias is_leaf? leaf? # @todo: Aliased for eventual replacement
170+
165171 # @!attribute [r] parentage
166172 # An array of ancestors of this node in reversed order
167173 # (the first element is the immediate parent of this node).
@@ -171,7 +177,7 @@ def is_leaf?
171177 # @return [Array<Tree::TreeNode>] An array of ancestors of this node
172178 # @return [nil] if this is a root node.
173179 def parentage
174- return nil if is_root ?
180+ return nil if root ?
175181
176182 parentage_array = [ ]
177183 prev_parent = parent
@@ -182,16 +188,18 @@ def parentage
182188 parentage_array
183189 end
184190
185- # @!attribute [r] has_children ?
191+ # @!attribute [r] children ?
186192 # +true+ if the this node has any child node.
187193 #
188194 # @return [Boolean] +true+ if child nodes exist.
189195 #
190- # @see #is_leaf ?
191- def has_children ?
196+ # @see #leaf ?
197+ def children ?
192198 !@children . empty?
193199 end
194200
201+ alias has_children? children? # @todo: Aliased for eventual replacement
202+
195203 # @!group Node Creation
196204
197205 # Creates a new node with a name and optional content.
@@ -266,7 +274,7 @@ def marshal_dump
266274 # a hash.
267275 def create_dump_rep # :nodoc:
268276 { name : @name ,
269- parent : ( is_root ? ? nil : @parent . name ) ,
277+ parent : ( root ? ? nil : @parent . name ) ,
270278 content : Marshal . dump ( @content ) }
271279 end
272280
@@ -311,7 +319,7 @@ def marshal_load(dumped_tree_array)
311319 # @return [String] A string representation of the node.
312320 def to_s
313321 "Node Name: #{ @name } Content: #{ @content . to_s || '<Empty>' } " \
314- "Parent: #{ is_root ? ? '<None>' : @parent . name . to_s } " \
322+ "Parent: #{ root ? ? '<None>' : @parent . name . to_s } " \
315323 "Children: #{ @children . length } Total Nodes: #{ size } "
316324 end
317325
@@ -425,7 +433,7 @@ def insertion_range
425433 def rename ( new_name )
426434 old_name = @name
427435
428- if is_root ?
436+ if root ?
429437 self . name = new_name
430438 else
431439 @parent . rename_child old_name , new_name
@@ -519,7 +527,7 @@ def parent=(parent) # :nodoc:
519527 #
520528 # @see #remove_all!
521529 def remove_from_parent!
522- @parent . remove! ( self ) unless is_root ?
530+ @parent . remove! ( self ) unless root ?
523531 end
524532
525533 # Removes all children from this node. If an independent reference exists to
@@ -657,7 +665,7 @@ def postordered_each
657665
658666 until node_stack . empty?
659667 peek_node = node_stack [ 0 ]
660- if peek_node . node . has_children ? && !peek_node . visited
668+ if peek_node . node . children ? && !peek_node . visited
661669 peek_node . visited = true
662670 # Add the children to the stack. Use the marking structure.
663671 marked_children =
@@ -737,10 +745,10 @@ def children(&block)
737745 # noinspection RubyUnusedLocalVariable
738746 def each_leaf
739747 if block_given?
740- each { |node | yield ( node ) if node . is_leaf ? }
748+ each { |node | yield ( node ) if node . leaf ? }
741749 self
742750 else
743- self . select ( &:is_leaf ? )
751+ self . select ( &:leaf ? )
744752 end
745753 end
746754
@@ -798,22 +806,24 @@ def last_child
798806 #
799807 # @return [Tree::TreeNode] The first sibling node.
800808 #
801- # @see #is_first_sibling ?
809+ # @see #first_sibling ?
802810 # @see #last_sibling
803811 def first_sibling
804- is_root ? ? self : parent . children . first
812+ root ? ? self : parent . children . first
805813 end
806814
807815 # Returns +true+ if this node is the first sibling at its level.
808816 #
809817 # @return [Boolean] +true+ if this is the first sibling.
810818 #
811- # @see #is_last_sibling ?
819+ # @see #last_sibling ?
812820 # @see #first_sibling
813- def is_first_sibling ?
821+ def first_sibling ?
814822 first_sibling == self
815823 end
816824
825+ alias is_first_sibling? first_sibling? # @todo: Aliased for eventual replacement
826+
817827 # Last sibling of this node. If this is the root node, then returns
818828 # itself.
819829 #
@@ -824,22 +834,24 @@ def is_first_sibling?
824834 #
825835 # @return [Tree::TreeNode] The last sibling node.
826836 #
827- # @see #is_last_sibling ?
837+ # @see #last_sibling ?
828838 # @see #first_sibling
829839 def last_sibling
830- is_root ? ? self : parent . children . last
840+ root ? ? self : parent . children . last
831841 end
832842
833843 # Returns +true+ if this node is the last sibling at its level.
834844 #
835845 # @return [Boolean] +true+ if this is the last sibling.
836846 #
837- # @see #is_first_sibling ?
847+ # @see #first_sibling ?
838848 # @see #last_sibling
839- def is_last_sibling ?
849+ def last_sibling ?
840850 last_sibling == self
841851 end
842852
853+ alias is_last_sibling? last_sibling? # @todo: Aliased for eventual replacement
854+
843855 # An array of siblings for this node. This node is excluded.
844856 #
845857 # If a block is provided, yields each of the sibling nodes to the block.
@@ -859,7 +871,7 @@ def siblings
859871 parent . children . each { |sibling | yield sibling if sibling != self }
860872 self
861873 else
862- return [ ] if is_root ?
874+ return [ ] if root ?
863875
864876 siblings = [ ]
865877 parent . children do |my_sibling |
@@ -876,10 +888,12 @@ def siblings
876888 # @return [Boolean] +true+ if this is the only child of its parent.
877889 #
878890 # @see #siblings
879- def is_only_child ?
880- is_root ? ? true : parent . children . size == 1
891+ def only_child ?
892+ root ? ? true : parent . children . size == 1
881893 end
882894
895+ alias is_only_child? only_child? # @todo: Aliased for eventual replacement
896+
883897 # Next sibling for this node.
884898 # The _next_ node is defined as the node to right of this node.
885899 #
@@ -891,7 +905,7 @@ def is_only_child?
891905 # @see #previous_sibling
892906 # @see #siblings
893907 def next_sibling
894- return nil if is_root ?
908+ return nil if root ?
895909
896910 idx = parent . children . index ( self )
897911 parent . children . at ( idx + 1 ) if idx
@@ -908,7 +922,7 @@ def next_sibling
908922 # @see #next_sibling
909923 # @see #siblings
910924 def previous_sibling
911- return nil if is_root ?
925+ return nil if root ?
912926
913927 idx = parent . children . index ( self )
914928 parent . children . at ( idx - 1 ) if idx &.positive?
@@ -943,14 +957,14 @@ def print_tree(level = node_depth, max_depth = nil,
943957 } )
944958 prefix = '' . dup # dup NEEDs to be invoked to make this mutable.
945959
946- if is_root ?
960+ if root ?
947961 prefix << '*'
948962 else
949- prefix << '|' unless parent . is_last_sibling ?
963+ prefix << '|' unless parent . last_sibling ?
950964 prefix << ( ' ' * ( level - 1 ) * 4 )
951- prefix << ( is_last_sibling ? ? '+' : '|' )
965+ prefix << ( last_sibling ? ? '+' : '|' )
952966 prefix << '---'
953- prefix << ( has_children ? ? '+' : '>' )
967+ prefix << ( children ? ? '+' : '>' )
954968 end
955969
956970 block . call ( self , prefix )
0 commit comments