Skip to content

Commit fba400a

Browse files
committed
Normalize Ruby style and docs
Clean up line continuations, block forwarding, and memoization naming across tree helpers while keeping behavior unchanged. Update documentation/comments to match the refactored layout.
1 parent 3cc6d4c commit fba400a

7 files changed

Lines changed: 34 additions & 32 deletions

File tree

lib/tree.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,11 @@ def marshal_load(dumped_tree_array)
316316
# @return [String] A string representation of the node.
317317
def to_s
318318
content_str = @content.nil? ? '<Empty>' : @content.to_s
319-
"Node Name: #{@name} Content: #{content_str} " \
320-
"Parent: #{root? ? '<None>' : @parent.name.to_s} " \
319+
[
320+
"Node Name: #{@name} Content: #{content_str}",
321+
"Parent: #{root? ? '<None>' : @parent.name}",
321322
"Children: #{@children.length} Total Nodes: #{size}"
323+
].join(' ')
322324
end
323325

324326
# Returns the requested node from the set of immediate children.
@@ -354,6 +356,7 @@ def [](name_or_index)
354356
@children_hash[name_or_index]
355357
end
356358
end
359+
357360
# Provides a comparison operation for the nodes.
358361
#
359362
# Comparison is based on the natural ordering of the node name objects.

lib/tree/binarytree.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def right_child?
119119
def add(child)
120120
raise ArgumentError, 'Already has two child nodes' if @children.size == 2
121121

122-
super(child)
122+
super
123123
end
124124

125125
# Instantiate and insert child nodes from data in a Ruby +Hash+
@@ -152,10 +152,9 @@ def add(child)
152152
# @raise [ArgumentError] This exception is raised if a non-hash is passed.
153153
# @return [Array] Array of child nodes added
154154
def add_from_hash(hashed_subtree)
155-
raise ArgumentError, 'Too many children'\
156-
if hashed_subtree.size + @children.size > 2
155+
raise ArgumentError, 'Too many children' if hashed_subtree.size + @children.size > 2
157156

158-
super(hashed_subtree)
157+
super
159158
end
160159

161160
# Performs in-order traversal (including this node).
@@ -203,8 +202,8 @@ def inordered_each
203202
#
204203
# @raise [ArgumentError] If the index is out of limits.
205204
def set_child_at(child, at_index)
206-
raise ArgumentError, 'A binary tree cannot have more than two children.'\
207-
unless (0..1).include? at_index
205+
raise ArgumentError,
206+
'A binary tree cannot have more than two children.' unless (0..1).include? at_index
208207

209208
old_child = @children[at_index]
210209
if old_child && old_child != child

lib/tree/utils/hash_converter.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,9 @@ module ClassMethods
9898
# values that are not hashes or nils.
9999

100100
def from_hash(hash)
101-
raise ArgumentError, 'Argument must be a type of hash'\
102-
unless hash.is_a?(Hash)
101+
raise ArgumentError, 'Argument must be a type of hash' unless hash.is_a?(Hash)
103102

104-
raise ArgumentError, 'Hash must have one top-level element'\
105-
if hash.size != 1
103+
raise ArgumentError, 'Hash must have one top-level element' if hash.size != 1
106104

107105
root, children = hash.first
108106

@@ -147,8 +145,7 @@ def from_hash(hash)
147145
# @return [Array] Array of child nodes added
148146
# @see ClassMethods#from_hash
149147
def add_from_hash(children)
150-
raise ArgumentError, 'Argument must be a type of hash'\
151-
unless children.is_a?(Hash)
148+
raise ArgumentError, 'Argument must be a type of hash' unless children.is_a?(Hash)
152149

153150
child_nodes = []
154151
children.each do |child, grandchildren|

lib/tree/utils/metrics_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module TreeMetricsHandler
5252
#
5353
# @return [Integer] Total number of nodes in this (sub)tree.
5454
def size
55-
@node_size ||= count
55+
@size ||= count
5656
end
5757

5858
# @!attribute [r] length

lib/tree/utils/navigation_methods.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ def children?
4343
alias has_children? children? # @todo: Aliased for eventual replacement
4444

4545
# An array of all the immediate children of this node.
46-
def children(&block)
46+
def children(&)
4747
if block_given?
48-
@children.each(&block)
48+
@children.each(&)
4949
self
5050
else
5151
@children.clone
@@ -108,6 +108,7 @@ def siblings
108108
siblings = []
109109
parent.send(:children_array).each do |my_sibling|
110110
next unless my_sibling
111+
111112
siblings << my_sibling if my_sibling != self
112113
end
113114
siblings
@@ -116,7 +117,7 @@ def siblings
116117

117118
# +true+ if this node is the only child of its parent.
118119
def only_child?
119-
root? ? true : parent.send(:children_array).count { |child| child } == 1
120+
root? || parent.send(:children_array).one? { |child| child }
120121
end
121122

122123
alias is_only_child? only_child? # @todo: Aliased for eventual replacement

lib/tree/utils/structure_methods.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,20 @@ def add(child, at_index = -1)
5353

5454
# Lazy man's unique test, won't test if children of child are unique in
5555
# this tree too.
56-
raise "Child #{child.name} already added!"\
57-
if @children_hash.include?(child.name)
56+
raise "Child #{child.name} already added!" if @children_hash.include?(child.name)
5857

5958
child.parent&.remove! child # Detach from the old parent
6059

6160
if insertion_range.include?(at_index)
6261
@children.insert(at_index, child)
6362
else
64-
raise 'Attempting to insert a child at a non-existent location'\
65-
" (#{at_index}) "\
66-
'when only positions from '\
67-
"#{insertion_range.min} to #{insertion_range.max} exist."
63+
message = [
64+
'Attempting to insert a child at a non-existent location',
65+
"(#{at_index})",
66+
'when only positions from',
67+
"#{insertion_range.min} to #{insertion_range.max} exist."
68+
].join(' ')
69+
raise message
6870
end
6971

7072
@children_hash[child.name] = child
@@ -95,11 +97,11 @@ def rename(new_name)
9597

9698
# Renames the specified child node
9799
def rename_child(old_name, new_name)
98-
raise ArgumentError, "Invalid child name specified: #{old_name}"\
99-
unless @children_hash.key?(old_name)
100+
raise ArgumentError,
101+
"Invalid child name specified: #{old_name}" unless @children_hash.key?(old_name)
100102

101-
raise ArgumentError, "Child name already exists: #{new_name}"\
102-
if @children_hash.key?(new_name)
103+
raise ArgumentError,
104+
"Child name already exists: #{new_name}" if @children_hash.key?(new_name)
103105

104106
@children_hash[new_name] = @children_hash.delete(old_name)
105107
@children_hash[new_name].name = new_name
@@ -180,7 +182,7 @@ def clear_root_cache!
180182
def invalidate_size_cache_upwards!
181183
node = self
182184
while node
183-
node.instance_variable_set(:@node_size, nil)
185+
node.instance_variable_set(:@size, nil)
184186
node = node.parent
185187
end
186188
end

lib/tree/utils/traversal_methods.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def each # :yields: node
5555
end
5656

5757
# Traverses the (sub)tree rooted at this node in pre-ordered sequence.
58-
def preordered_each(&block) # :yields: node
59-
each(&block)
58+
def preordered_each(&) # :yields: node
59+
each(&)
6060
end
6161

6262
# Traverses the (sub)tree rooted at this node in post-ordered sequence.
@@ -137,7 +137,7 @@ def print_tree(level = node_depth, max_depth = nil,
137137
block = lambda { |node, prefix|
138138
puts "#{prefix} #{node.name}"
139139
})
140-
prefix = ''.dup # dup NEEDs to be invoked to make this mutable.
140+
prefix = ''.dup # dup must be invoked to make this mutable.
141141

142142
if root?
143143
prefix << '*'

0 commit comments

Comments
 (0)