Skip to content

Commit 3be0e58

Browse files
committed
Automatically fixed the 'safe' rubocop issues.
1 parent 833c6b3 commit 3be0e58

16 files changed

Lines changed: 273 additions & 298 deletions

Rakefile

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ task :version do
5555
end
5656

5757
require 'rake/clean'
58-
task :clean => 'gem:clobber_package'
58+
task clean: 'gem:clobber_package'
5959
CLEAN.include('coverage')
60-
task :clobber => [:clean, 'doc:clobber_rdoc', 'doc:clobber_yard']
60+
task clobber: [:clean, 'doc:clobber_rdoc', 'doc:clobber_yard']
6161

6262
desc 'Open an irb session preloaded with this library'
6363
task :console do
@@ -83,7 +83,7 @@ namespace :doc do # ................................ Documentation
8383
require 'yard'
8484
YARD::Rake::YardocTask.new do |t|
8585
t.files = ['lib/**/*.rb', '-', GEM_SPEC.extra_rdoc_files]
86-
t.options = %w(--no-private --embed-mixins)
86+
t.options = %w[--no-private --embed-mixins]
8787
end
8888
rescue LoadError
8989
# Oh well.
@@ -96,7 +96,7 @@ namespace :doc do # ................................ Documentation
9696
end
9797

9898
desc 'Run the test cases'
99-
task :test => 'test:unit'
99+
task test: 'test:unit'
100100

101101
namespace :test do # ................................ Test related
102102
require 'rake/testtask'
@@ -144,15 +144,13 @@ rescue LoadError
144144
end
145145

146146
namespace :tag do # ................................ Emacs Tags
147-
begin
148-
require 'rtagstask'
149-
RTagsTask.new(:tags) do |rd|
150-
rd.vi = false
151-
CLEAN.include('TAGS')
152-
end
153-
rescue LoadError
154-
# Oh well. Can't have everything.
147+
require 'rtagstask'
148+
RTagsTask.new(:tags) do |rd|
149+
rd.vi = false
150+
CLEAN.include('TAGS')
155151
end
152+
rescue LoadError
153+
# Oh well. Can't have everything.
156154
end
157155

158156
namespace :gem do # ................................ Gem related
@@ -163,7 +161,7 @@ namespace :gem do # ................................ Gem related
163161
end
164162

165163
desc 'Push the gem into the Rubygems repository'
166-
task :push => :gem do
164+
task push: :gem do
167165
sh "gem push pkg/#{GEM_NAME}"
168166
end
169167
end

gemfiles/Gemfile.rbx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
source 'https://rubygems.org'
22

33
# Specify your gem's dependencies in rubytree.gemspec
4-
gemspec :path => '..'
4+
gemspec path: '..'
55

66
platforms :rbx do
7-
gem "rubysl"
8-
gem "rubysl-test-unit"
7+
gem 'rubysl'
8+
gem 'rubysl-test-unit'
99
end
1010

1111
group :development, :test do
12-
gem "rake", "~> 10.1"
12+
gem 'rake', '~> 10.1'
1313
end
1414

1515
# Local Variables:

lib/rubytree.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@
3838
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3939
#
4040

41-
require 'tree.rb'
41+
require 'tree'

lib/tree.rb

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ class TreeNode
108108
# +content+ attribute for any non-unique node requirements.
109109
#
110110
# If you want to change the name, you probably want to call +rename+
111-
# instead.
111+
# instead. Note that +name=+ is a protected method.
112112
#
113113
# @see content
114114
# @see rename
115-
attr_reader :name
115+
attr_accessor :name
116116

117117
# @!attribute [rw] content
118118
# Content of this node. Can be +nil+. Note that there is no
@@ -217,7 +217,8 @@ def has_children?
217217
def initialize(name, content = nil)
218218
raise ArgumentError, 'Node name HAS to be provided!' if name.nil?
219219

220-
@name, @content = name, content
220+
@name = name
221+
@content = content
221222

222223
if name.is_a?(Integer)
223224
warn StructuredWarnings::StandardWarning,
@@ -308,7 +309,7 @@ def marshal_load(dumped_tree_array)
308309
#
309310
# @return [String] A string representation of the node.
310311
def to_s
311-
"Node Name: #{@name} Content: #{(@content.to_s || '<Empty>')} Parent: #{(is_root? ? '<None>' : @parent.name.to_s)} Children: #{@children.length} Total Nodes: #{size}"
312+
"Node Name: #{@name} Content: #{@content.to_s || '<Empty>'} Parent: #{is_root? ? '<None>' : @parent.name.to_s} Children: #{@children.length} Total Nodes: #{size}"
312313
end
313314

314315
# @!group Structure Modification
@@ -445,14 +446,6 @@ def rename_child(old_name, new_name)
445446
@children_hash[new_name].name = new_name
446447
end
447448

448-
# Protected method to set the name of this node.
449-
# This method should *NOT* be invoked by client code.
450-
#
451-
# @param [Object] new_name The node Name to set.
452-
#
453-
# @return [Object] The new name.
454-
attr_writer :name
455-
456449
# Replaces the specified child node with another child node on this node.
457450
#
458451
# @param [Tree::TreeNode] old_child The child node to be replaced.
@@ -625,18 +618,18 @@ def [](name_or_index, num_as_name = false)
625618
# @return [Tree::TreeNode] this node, if a block if given
626619
# @return [Enumerator] an enumerator on this tree, if a block is *not* given
627620
# noinspection RubyUnusedLocalVariable
628-
def each(&block) # :yields: node
621+
def each # :yields: node
629622
return to_enum unless block_given?
630623

631624
node_stack = [self] # Start with this node
632625

633626
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
627+
current = node_stack.shift # Pop the top-most node
628+
next unless current # Might be 'nil' (esp. for binary trees)
629+
630+
yield current # and process it
631+
# Stack children of the current node at top of the stack
632+
node_stack = current.children.concat(node_stack)
640633
end
641634

642635
self if block_given?
@@ -666,7 +659,7 @@ def preordered_each(&block) # :yields: node
666659
# @return [Tree::TreeNode] this node, if a block if given
667660
# @return [Enumerator] an enumerator on this tree, if a block is *not* given
668661
# noinspection RubyUnusedLocalVariable
669-
def postordered_each(&block)
662+
def postordered_each
670663
return to_enum(:postordered_each) unless block_given?
671664

672665
# Using a marked node in order to skip adding the children of nodes that
@@ -705,7 +698,7 @@ def postordered_each(&block)
705698
# @return [Tree::TreeNode] this node, if a block if given
706699
# @return [Enumerator] an enumerator on this tree, if a block is *not* given
707700
# noinspection RubyUnusedLocalVariable
708-
def breadth_each(&block)
701+
def breadth_each
709702
return to_enum(:breadth_each) unless block_given?
710703

711704
node_queue = [self] # Create a queue with self as the initial entry
@@ -733,9 +726,9 @@ def breadth_each(&block)
733726
#
734727
# @return [Array<Tree::TreeNode>] An array of the child nodes, if no block
735728
# is given.
736-
def children
729+
def children(&block)
737730
if block_given?
738-
@children.each { |child| yield child }
731+
@children.each(&block)
739732
self
740733
else
741734
@children.clone
@@ -757,7 +750,7 @@ def children
757750
# @return [Tree::TreeNode] this node, if a block if given
758751
# @return [Array<Tree::TreeNode>] An array of the leaf nodes
759752
# noinspection RubyUnusedLocalVariable
760-
def each_leaf(&block)
753+
def each_leaf
761754
if block_given?
762755
each { |node| yield(node) if node.is_leaf? }
763756
self
@@ -775,7 +768,7 @@ def each_leaf(&block)
775768
#
776769
# @return [Tree::TreeNode] this node, if a block if given
777770
# @return [Enumerator] an enumerator on this tree, if a block is *not* given
778-
def each_level &block
771+
def each_level
779772
if block_given?
780773
level = [self]
781774
until level.empty?
@@ -884,9 +877,9 @@ def siblings
884877
return [] if is_root?
885878

886879
siblings = []
887-
parent.children { |my_sibling|
880+
parent.children do |my_sibling|
888881
siblings << my_sibling if my_sibling != self
889-
}
882+
end
890883
siblings
891884
end
892885
end
@@ -980,10 +973,11 @@ def print_tree(level = node_depth, max_depth = nil,
980973
# Exit if the max level is defined, and reached.
981974
return unless max_depth.nil? || level < max_depth
982975

983-
children { |child|
976+
# Child might be 'nil'
977+
children do |child|
984978
child&.print_tree(level + 1,
985979
max_depth, block)
986-
} # Child might be 'nil'
980+
end
987981
end
988982
end
989983
end

lib/tree/binarytree.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,13 @@ def add_from_hash(hashed_subtree)
170170
# @see #preordered_each
171171
# @see #postordered_each
172172
# noinspection RubyUnusedLocalVariable
173-
def inordered_each(&block)
174-
return self.to_enum unless block_given?
173+
def inordered_each
174+
return to_enum unless block_given?
175175

176176
node_stack = []
177177
current_node = self
178178

179-
until node_stack.empty? and current_node == nil
179+
until node_stack.empty? and current_node.nil?
180180
if current_node
181181
node_stack.push(current_node)
182182
current_node = current_node.left_child
@@ -243,7 +243,7 @@ def right_child=(child)
243243

244244
# Swaps the left and right child nodes of the receiver node with each other.
245245
def swap_children
246-
self.left_child, self.right_child = self.right_child, self.left_child
246+
self.left_child, self.right_child = right_child, left_child
247247
end
248248
end
249249
end

lib/tree/utils/camel_case_method_handler.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ module Tree::Utils
4343
# Provides utility functions to handle CamelCase methods, and redirect
4444
# invocation of such methods to the snake_case equivalents.
4545
module CamelCaseMethodHandler
46-
def self.included(base)
46+
def self.included(_base)
4747
# @!visibility private
4848
# Allow the deprecated CamelCase method names. Display a warning.
4949
# :nodoc:
5050
def method_missing(meth, *args, &blk)
51-
if self.respond_to?((new_method_name = to_snake_case(meth)))
51+
if respond_to?((new_method_name = to_snake_case(meth)))
5252
warn StructuredWarnings::DeprecatedMethodWarning,
5353
'The camelCased methods are deprecated. ' +
5454
"Please use #{new_method_name} instead of #{meth}"

lib/tree/utils/hash_converter.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,9 @@ def from_hash(hash)
100100

101101
root, children = hash.first
102102

103-
unless [Hash, NilClass].include?(children.class)
104-
raise ArgumentError, 'Invalid child. Must be nil or hash.'
105-
end
103+
raise ArgumentError, 'Invalid child. Must be nil or hash.' unless [Hash, NilClass].include?(children.class)
106104

107-
node = self.new(*root)
105+
node = new(*root)
108106
node.add_from_hash(children) unless children.nil?
109107
node
110108
end

lib/tree/utils/json_converter.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,14 @@ def self.included(base)
6262
# @see #to_json
6363
# @see http://stackoverflow.com/a/6880638/273808
6464
# noinspection RubyUnusedLocalVariable
65-
def as_json(options = {})
65+
def as_json(_options = {})
6666
json_hash = {
6767
name: name,
6868
content: content,
6969
JSON.create_id => self.class.name
7070
}
7171

72-
if has_children?
73-
json_hash['children'] = children
74-
end
72+
json_hash['children'] = children if has_children?
7573

7674
json_hash
7775
end
@@ -116,9 +114,11 @@ module ClassMethods
116114
def json_create(json_hash)
117115
node = new(json_hash['name'], json_hash['content'])
118116

119-
json_hash['children'].each do |child|
120-
node << child
121-
end if json_hash['children']
117+
if json_hash['children']
118+
json_hash['children'].each do |child|
119+
node << child
120+
end
121+
end
122122

123123
node
124124
end

lib/tree/utils/metrics_methods.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module Tree::Utils
4343
# Provides utility functions to measure various tree metrics.
4444
module TreeMetricsHandler
4545
# noinspection RubyUnusedLocalVariable
46-
def self.included(base)
46+
def self.included(_base)
4747
# @!group Metrics and Measures
4848

4949
# @!attribute [r] size
@@ -67,7 +67,7 @@ def size
6767
# @return [Integer] The total number of nodes in this (sub)tree.
6868
# @see #size
6969
def length
70-
self.size
70+
size
7171
end
7272

7373
# @!attribute [r] node_height

lib/tree/utils/path_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module Tree::Utils
4242
# Provides utility methods for path extraction
4343
module TreePathHandler
4444
# noinspection RubyUnusedLocalVariable
45-
def self.included(base)
45+
def self.included(_base)
4646
# @!group Node Path
4747

4848
# Returns the path of this node from the root as a string, with the node

0 commit comments

Comments
 (0)