Skip to content

Commit 833c6b3

Browse files
committed
Automatic formatting fixes using rubocop.
1 parent 074be2e commit 833c6b3

19 files changed

Lines changed: 400 additions & 442 deletions

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Local configuration for the rubocop linter.

Rakefile

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ task :console do
6464
sh 'irb -rubygems -r ./lib/tree.rb'
6565
end
6666

67-
namespace :doc do # ................................ Documentation
67+
namespace :doc do # ................................ Documentation
6868
begin
6969
gem 'rdoc', '>= 2.4.2' # To get around a stupid bug in Ruby 1.9.2 Rake.
7070
require 'rdoc/task'
@@ -98,8 +98,7 @@ end
9898
desc 'Run the test cases'
9999
task :test => 'test:unit'
100100

101-
namespace :test do # ................................ Test related
102-
101+
namespace :test do # ................................ Test related
103102
require 'rake/testtask'
104103
Rake::TestTask.new(:unit) do |test|
105104
test.libs << 'lib' << 'test'
@@ -131,10 +130,9 @@ namespace :test do # ................................ Test related
131130
rescue LoadError
132131
# Oh well. Can't have everything.
133132
end
134-
135133
end
136134

137-
begin # ................................ rspec tests
135+
begin # ................................ rspec tests
138136
require 'rspec/core/rake_task'
139137

140138
RSpec::Core::RakeTask.new(:spec) do |t|
@@ -145,7 +143,7 @@ rescue LoadError
145143
# Cannot load rspec.
146144
end
147145

148-
namespace :tag do # ................................ Emacs Tags
146+
namespace :tag do # ................................ Emacs Tags
149147
begin
150148
require 'rtagstask'
151149
RTagsTask.new(:tags) do |rd|
@@ -155,7 +153,7 @@ end
155153
rescue LoadError
156154
# Oh well. Can't have everything.
157155
end
158-
end
156+
end
159157

160158
namespace :gem do # ................................ Gem related
161159
require 'rubygems/package_task'

examples/example_basic.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# +---------------+
2424

2525
# ..... Example starts.
26-
require 'tree' # Load the library
26+
require 'tree' # Load the library
2727

2828
# ..... Create the root node first. Note that every node has a name and an optional content payload.
2929
root_node = Tree::TreeNode.new('ROOT', 'Root Content')

lib/tree.rb

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
# This module also acts as the namespace for all classes in the *RubyTree*
4848
# package.
4949
module Tree
50-
5150
# == TreeNode Class Description
5251
#
5352
# This class models the nodes for an *N-ary* tree data structure. The
@@ -267,10 +266,9 @@ def marshal_dump
267266
# Creates a dump representation of this node and returns the same as
268267
# a hash.
269268
def create_dump_rep # :nodoc:
270-
{name: @name,
271-
parent: (is_root? ? nil : @parent.name),
272-
content: Marshal.dump(@content)
273-
}
269+
{ name: @name,
270+
parent: (is_root? ? nil : @parent.name),
271+
content: Marshal.dump(@content) }
274272
end
275273

276274
protected :create_dump_rep
@@ -285,7 +283,7 @@ def create_dump_rep # :nodoc:
285283
# self and makes itself the root.
286284
#
287285
def marshal_load(dumped_tree_array)
288-
nodes = { }
286+
nodes = {}
289287
dumped_tree_array.each do |node_hash|
290288
name = node_hash[:name]
291289
parent_name = node_hash[:parent]
@@ -381,7 +379,7 @@ def add(child, at_index = -1)
381379
raise ArgumentError, 'Attempting to add a nil node' unless child
382380

383381
raise ArgumentError, 'Attempting add node to itself' if equal?(child)
384-
382+
385383
raise ArgumentError, 'Attempting add root as a child' if child.equal?(root)
386384

387385
# Lazy man's unique test, won't test if children of child are unique in
@@ -408,7 +406,7 @@ def add(child, at_index = -1)
408406
# Return a range of valid insertion positions. Used in the #add method.
409407
def insertion_range
410408
max = @children.size
411-
min = -(max+1)
409+
min = -(max + 1)
412410
min..max
413411
end
414412

@@ -444,7 +442,7 @@ def rename_child(old_name, new_name)
444442
unless @children_hash.key?(old_name)
445443

446444
@children_hash[new_name] = @children_hash.delete(old_name)
447-
@children_hash[new_name].name=new_name
445+
@children_hash[new_name].name = new_name
448446
end
449447

450448
# Protected method to set the name of this node.
@@ -558,7 +556,7 @@ def set_as_root! # :nodoc:
558556
# The nodes become immutable after this operation. In effect, the entire tree's
559557
# structure and contents become _read-only_ and cannot be changed.
560558
def freeze_tree!
561-
each {|node| node.freeze}
559+
each { |node| node.freeze }
562560
end
563561

564562
# @!endgroup
@@ -598,7 +596,7 @@ def freeze_tree!
598596
#
599597
# @see #add
600598
# @see #initialize
601-
def [](name_or_index, num_as_name=false)
599+
def [](name_or_index, num_as_name = false)
602600
raise ArgumentError, 'Name_or_index needs to be provided!' if name_or_index.nil?
603601

604602
if name_or_index.is_a?(Integer) && !num_as_name
@@ -628,21 +626,20 @@ def [](name_or_index, num_as_name=false)
628626
# @return [Enumerator] an enumerator on this tree, if a block is *not* given
629627
# noinspection RubyUnusedLocalVariable
630628
def each(&block) # :yields: node
629+
return to_enum unless block_given?
631630

632-
return to_enum unless block_given?
631+
node_stack = [self] # Start with this node
633632

634-
node_stack = [self] # Start with this node
635-
636-
until node_stack.empty?
637-
current = node_stack.shift # Pop the top-most node
638-
if current # Might be 'nil' (esp. for binary trees)
639-
yield current # and process it
640-
# Stack children of the current node at top of the stack
641-
node_stack = current.children.concat(node_stack)
642-
end
643-
end
633+
until node_stack.empty?
634+
current = node_stack.shift # Pop the top-most node
635+
if current # Might be 'nil' (esp. for binary trees)
636+
yield current # and process it
637+
# Stack children of the current node at top of the stack
638+
node_stack = current.children.concat(node_stack)
639+
end
640+
end
644641

645-
self if block_given?
642+
self if block_given?
646643
end
647644

648645
# Traverses the (sub)tree rooted at this node in pre-ordered sequence.
@@ -684,7 +681,7 @@ def postordered_each(&block)
684681
peek_node.visited = true
685682
# Add the children to the stack. Use the marking structure.
686683
marked_children =
687-
peek_node.node.children.map {|node| marked_node.new(node, false)}
684+
peek_node.node.children.map { |node| marked_node.new(node, false) }
688685
node_stack = marked_children.concat(node_stack)
689686
next
690687
else
@@ -738,7 +735,7 @@ def breadth_each(&block)
738735
# is given.
739736
def children
740737
if block_given?
741-
@children.each {|child| yield child}
738+
@children.each { |child| yield child }
742739
self
743740
else
744741
@children.clone
@@ -887,8 +884,9 @@ def siblings
887884
return [] if is_root?
888885

889886
siblings = []
890-
parent.children {|my_sibling|
891-
siblings << my_sibling if my_sibling != self}
887+
parent.children { |my_sibling|
888+
siblings << my_sibling if my_sibling != self
889+
}
892890
siblings
893891
end
894892
end
@@ -963,7 +961,8 @@ def <=>(other)
963961
# @param [Proc] block optional block to use for rendering
964962
def print_tree(level = node_depth, max_depth = nil,
965963
block = lambda { |node, prefix|
966-
puts "#{prefix} #{node.name}" })
964+
puts "#{prefix} #{node.name}"
965+
})
967966
prefix = ''
968967

969968
if is_root?
@@ -983,8 +982,8 @@ def print_tree(level = node_depth, max_depth = nil,
983982

984983
children { |child|
985984
child&.print_tree(level + 1,
986-
max_depth, block) } # Child might be 'nil'
985+
max_depth, block)
986+
} # Child might be 'nil'
987987
end
988-
989988
end
990989
end

lib/tree/binarytree.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
require_relative '../tree'
4242

4343
module Tree
44-
4544
# Provides a Binary tree implementation. This node allows only two child nodes
4645
# (left and right child). It also provides direct access to the left or right
4746
# child, including assignment to the same.
@@ -51,7 +50,6 @@ module Tree
5150
# @author Anupam Sengupta
5251
#
5352
class BinaryTreeNode < TreeNode
54-
5553
# @!group Core Attributes
5654

5755
# @!attribute [rw] left_child
@@ -85,6 +83,7 @@ def right_child
8583
# @return [Boolean] +true+ if this is the left child of its parent.
8684
def is_left_child?
8785
return false if is_root?
86+
8887
self == parent.left_child
8988
end
9089

@@ -95,6 +94,7 @@ def is_left_child?
9594
# @return [Boolean] +true+ if this is the right child of its parent.
9695
def is_right_child?
9796
return false if is_root?
97+
9898
self == parent.right_child
9999
end
100100

@@ -119,7 +119,6 @@ def add(child)
119119
super(child)
120120
end
121121

122-
123122
# Instantiate and insert child nodes from data in a Ruby +Hash+
124123
#
125124
# This method is used in conjunction with {Tree::TreeNode.from_hash} to
@@ -152,6 +151,7 @@ def add(child)
152151
def add_from_hash(hashed_subtree)
153152
raise ArgumentError, 'Too many children'\
154153
if hashed_subtree.size + @children.size > 2
154+
155155
super(hashed_subtree)
156156
end
157157

@@ -171,7 +171,6 @@ def add_from_hash(hashed_subtree)
171171
# @see #postordered_each
172172
# noinspection RubyUnusedLocalVariable
173173
def inordered_each(&block)
174-
175174
return self.to_enum unless block_given?
176175

177176
node_stack = []
@@ -189,7 +188,6 @@ def inordered_each(&block)
189188
end
190189

191190
self if block_given?
192-
193191
end
194192

195193
# A protected method to allow insertion of child nodes at the specified
@@ -247,7 +245,5 @@ def right_child=(child)
247245
def swap_children
248246
self.left_child, self.right_child = self.right_child, self.left_child
249247
end
250-
251248
end
252-
253249
end

lib/tree/utils/camel_case_method_handler.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,13 @@ def method_missing(meth, *args, &blk)
6666
def to_snake_case(camel_cased_word)
6767
word = camel_cased_word.to_s
6868
word.gsub!(/::/, '/')
69-
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
70-
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
69+
word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
70+
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
7171
word.tr!('-', '_')
7272
word.downcase!
7373
word
7474
end
7575
protected :to_snake_case
76-
7776
end # self.included
7877
end
7978
end

0 commit comments

Comments
 (0)