Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63f3e97

Browse files
authored
Use Safe Navigation (&.) from Ruby 2.3 (#265)
We can simplify our code by using `&.`.
1 parent d427fc5 commit 63f3e97

File tree

5 files changed

+7
-20
lines changed

5 files changed

+7
-20
lines changed

lib/rexml/attribute.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,7 @@ def to_string
130130
end
131131

132132
def doctype
133-
if @element
134-
doc = @element.document
135-
doc.doctype if doc
136-
end
133+
@element&.document&.doctype
137134
end
138135

139136
# Returns the attribute value, with entities replaced

lib/rexml/child.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ def previous_sibling=(other)
8383
# Returns:: the document this child belongs to, or nil if this child
8484
# belongs to no document
8585
def document
86-
return parent.document unless parent.nil?
87-
nil
86+
parent&.document
8887
end
8988

9089
# This doesn't yet handle encodings

lib/rexml/doctype.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,11 @@ def write( output, indent=0, transitive=false, ie_hack=false )
171171
end
172172

173173
def context
174-
if @parent
175-
@parent.context
176-
else
177-
nil
178-
end
174+
@parent&.context
179175
end
180176

181177
def entity( name )
182-
@entities[name].unnormalized if @entities[name]
178+
@entities[name]&.unnormalized
183179
end
184180

185181
def add child
@@ -288,8 +284,7 @@ def initialize name, middle, pub, sys
288284
end
289285

290286
def to_s
291-
context = nil
292-
context = parent.context if parent
287+
context = parent&.context
293288
notation = "<!NOTATION #{@name}"
294289
reference_writer = ReferenceWriter.new(@middle, @public, @system, context)
295290
reference_writer.write(notation)

lib/rexml/element.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,7 @@ def root
473473
# Related: #root, #root_node.
474474
#
475475
def document
476-
rt = root
477-
rt.parent if rt
476+
root&.parent
478477
end
479478

480479
# :call-seq:

lib/rexml/text.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,7 @@ def <=>( other )
200200
end
201201

202202
def doctype
203-
if @parent
204-
doc = @parent.document
205-
doc.doctype if doc
206-
end
203+
@parent&.document&.doctype
207204
end
208205

209206
REFERENCE = /#{Entity::REFERENCE}/

0 commit comments

Comments
 (0)