Skip to content

Commit e36aa4c

Browse files
committed
Replace use of .inject with more efficient .each_with_object.
1 parent d172262 commit e36aa4c

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

lib/json/ld/compact.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def compact(element, property: nil)
218218
end
219219

220220
# Re-order result keys
221-
result.keys.kw_sort.inject({}) {|map, kk| map[kk] = result[kk]; map}
221+
result.keys.kw_sort.each_with_object({}) {|kk, memo| memo[kk] = result[kk]}
222222
else
223223
# For other types, the compacted value is the element value
224224
#log_debug("compact") {element.class.to_s}

lib/json/ld/context.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,9 +1576,10 @@ def remove_base(iri)
15761576
#
15771577
# @return [Array<RDF::URI>]
15781578
def mappings
1579-
term_definitions.inject({}) do |memo, (t,td)|
1580-
memo[t] = td ? td.id : nil
1581-
memo
1579+
{}.tap do |memo|
1580+
term_definitions.each_pair do |t,td|
1581+
memo[t] = td ? td.id : nil
1582+
end
15821583
end
15831584
end
15841585

@@ -1598,9 +1599,10 @@ def mapping(term)
15981599
# @return [Array<String>]
15991600
# @deprecated
16001601
def languages
1601-
term_definitions.inject({}) do |memo, (t,td)|
1602-
memo[t] = td.language_mapping
1603-
memo
1602+
{}.tap do |memo|
1603+
term_definitions.each_pair do |t,td|
1604+
memo[t] = td.language_mapping
1605+
end
16041606
end
16051607
end
16061608

lib/json/ld/expand.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def expand(input, active_property, context, ordered: true)
113113

114114
# Re-order result keys if ordering
115115
if ordered
116-
output_object.keys.kw_sort.inject({}) {|map, kk| map[kk] = output_object[kk]; map}
116+
output_object.keys.kw_sort.each_with_object({}) {|kk, memo| memo[kk] = output_object[kk]}
117117
else
118118
output_object
119119
end

lib/json/ld/frame.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,9 @@ def cleanup_preserve(input, bnodes_to_clear)
264264
#
265265
# @return all of the matched subjects.
266266
def filter_subjects(state, subjects, frame, flags)
267-
subjects.inject({}) do |memo, id|
267+
subjects.each_with_object({}) do |id, memo|
268268
subject = state[:graphMap][state[:graph]][id]
269269
memo[id] = subject if filter_subject(subject, frame, state, flags)
270-
memo
271270
end
272271
end
273272

0 commit comments

Comments
 (0)