Skip to content

Commit d0a5958

Browse files
committed
Refactor: Avoid Hash[] pattern
Hash[] pattern creates many unnecessary intermediate array.
1 parent 8091ff6 commit d0a5958

10 files changed

+43
-40
lines changed

lib/roo/base.rb

+15-12
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,12 @@ def each(options = {})
288288
clean_sheet_if_need(options)
289289
search_or_set_header(options)
290290
headers = @headers ||
291-
Hash[(first_column..last_column).map do |col|
292-
[cell(@header_line, col), col]
293-
end]
291+
(first_column..last_column).each_with_object({}) do |col, hash|
292+
hash[cell(@header_line, col)] = col
293+
end
294294

295295
@header_line.upto(last_row) do |line|
296-
yield(Hash[headers.map { |k, v| [k, cell(line, v)] }])
296+
yield(headers.each_with_object({}) { |(k, v), hash| hash[k] = cell(line, v) })
297297
end
298298
end
299299
end
@@ -424,9 +424,9 @@ def find_by_row(row_index)
424424

425425
def find_by_conditions(options)
426426
rows = first_row.upto(last_row)
427-
header_for = Hash[1.upto(last_column).map do |col|
428-
[col, cell(@header_line, col)]
429-
end]
427+
header_for = 1.upto(last_column).each_with_object({}) do |col, hash|
428+
hash[col] = cell(@header_line, col)
429+
end
430430

431431
# are all conditions met?
432432
conditions = options[:conditions]
@@ -441,9 +441,9 @@ def find_by_conditions(options)
441441
rows.map { |i| row(i) }
442442
else
443443
rows.map do |i|
444-
Hash[1.upto(row(i).size).map do |j|
445-
[header_for.fetch(j), cell(i, j)]
446-
end]
444+
1.upto(row(i).size).each_with_object({}) do |j, hash|
445+
hash[header_for.fetch(j)] = cell(i, j)
446+
end
447447
end
448448
end
449449
end
@@ -497,8 +497,11 @@ def sanitize_value(v)
497497
def set_headers(hash = {})
498498
# try to find header row with all values or give an error
499499
# then create new hash by indexing strings and keeping integers for header array
500-
@headers = row_with(hash.values, true)
501-
@headers = Hash[hash.keys.zip(@headers.map { |x| header_index(x) })]
500+
header_row = row_with(hash.values, true)
501+
@headers = {}
502+
hash.each_with_index do |(key, _), index|
503+
@headers[key] = header_index(header_row[index])
504+
end
502505
end
503506

504507
def header_index(query)

lib/roo/excelx.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ def initialize(filename_or_stream, options = {})
6666
end
6767
end.compact
6868
@sheets = []
69-
@sheets_by_name = Hash[@sheet_names.map.with_index do |sheet_name, n|
70-
@sheets[n] = Sheet.new(sheet_name, @shared, n, sheet_options)
71-
[sheet_name, @sheets[n]]
72-
end]
69+
@sheets_by_name = {}
70+
@sheet_names.each_with_index do |sheet_name, n|
71+
@sheets_by_name[sheet_name] = @sheets[n] = Sheet.new(sheet_name, @shared, n, sheet_options)
72+
end
7373

7474
if cell_max
7575
cell_count = ::Roo::Utils.num_cells_in_range(sheet_for(options.delete(:sheet)).dimensions)

lib/roo/excelx/comments.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ def comments
1212
def extract_comments
1313
return {} unless doc_exists?
1414

15-
Hash[doc.xpath('//comments/commentList/comment').map do |comment|
15+
doc.xpath('//comments/commentList/comment').each_with_object({}) do |comment, hash|
1616
value = (comment.at_xpath('./text/r/t') || comment.at_xpath('./text/t')).text
17-
[::Roo::Utils.ref_to_key(comment.attributes['ref'].to_s), value]
18-
end]
17+
hash[::Roo::Utils.ref_to_key(comment.attributes['ref'].to_s)] = value
18+
end
1919
end
2020
end
2121
end

lib/roo/excelx/images.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ def list
1717
def extract_images_names
1818
return {} unless doc_exists?
1919

20-
Hash[doc.xpath('/Relationships/Relationship').map do |rel|
21-
[rel['Id'], "roo" + rel['Target'].gsub(/\.\.\/|\//, '_')]
22-
end]
20+
doc.xpath('/Relationships/Relationship').each_with_object({}) do |rel, hash|
21+
hash[rel['Id']] = "roo" + rel['Target'].gsub(/\.\.\/|\//, '_')
22+
end
2323
end
2424
end
2525
end

lib/roo/excelx/relationships.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ def to_a
1616
def extract_relationships
1717
return [] unless doc_exists?
1818

19-
Hash[doc.xpath('/Relationships/Relationship').map do |rel|
20-
[rel.attribute('Id').text, rel]
21-
end]
19+
doc.xpath('/Relationships/Relationship').each_with_object({}) do |rel, hash|
20+
hash[rel.attribute('Id').text] = rel
21+
end
2222
end
2323
end
2424
end

lib/roo/excelx/shared_strings.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ def create_html(text, formatting)
140140
tmp_str << "<#{elem}>" if val
141141
end
142142
tmp_str << text
143-
reverse_format = Hash[formatting.to_a.reverse]
144-
reverse_format.each do |elem, val|
143+
144+
formatting.reverse_each do |elem, val|
145145
tmp_str << "</#{elem}>" if val
146146
end
147147
tmp_str

lib/roo/excelx/sheet_doc.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,13 @@ def create_cell_from_value(value_type, cell, formula, format, style, hyperlink,
178178
def extract_hyperlinks(relationships)
179179
return {} unless (hyperlinks = doc.xpath('/worksheet/hyperlinks/hyperlink'))
180180

181-
Hash[hyperlinks.map do |hyperlink|
182-
if hyperlink['id'] && (relationship = relationships[hyperlink['id']])
181+
hyperlinks.each_with_object({}) do |hyperlink, hash|
182+
if relationship = relationships[hyperlink['id']]
183183
target_link = relationship['Target']
184184
target_link += "##{hyperlink['location']}" if hyperlink['location']
185-
[::Roo::Utils.ref_to_key(hyperlink['ref']), target_link]
185+
hash[::Roo::Utils.ref_to_key(hyperlink.attributes["ref"].to_s)] = target_link
186186
end
187-
end.compact]
187+
end
188188
end
189189

190190
def expand_merged_ranges(cells)

lib/roo/excelx/styles.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ def extract_num_fmt_ids
5555
end
5656

5757
def extract_num_fmts
58-
Hash[doc.xpath('//numFmt').map do |num_fmt|
59-
[num_fmt['numFmtId'], num_fmt['formatCode']]
60-
end]
58+
doc.xpath('//numFmt').each_with_object({}) do |num_fmt, hash|
59+
hash[num_fmt['numFmtId']] = num_fmt['formatCode']
60+
end
6161
end
6262
end
6363
end

lib/roo/excelx/workbook.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ def sheets
2929

3030
# aka labels
3131
def defined_names
32-
Hash[doc.xpath('//definedName').map do |defined_name|
32+
doc.xpath('//definedName').each_with_object({}) do |defined_name, hash|
3333
# "Sheet1!$C$5"
3434
sheet, coordinates = defined_name.text.split('!$', 2)
3535
col, row = coordinates.split('$')
3636
name = defined_name['name']
37-
[name, Label.new(name, sheet, row, col)]
38-
end]
37+
hash[name] = Label.new(name, sheet, row, col)
38+
end
3939
end
4040

4141
def base_timestamp

lib/roo/open_office.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -563,16 +563,16 @@ def read_comments(sheet = nil)
563563
end
564564

565565
def read_labels
566-
@label ||= Hash[doc.xpath('//table:named-range').map do |ne|
566+
@label ||= doc.xpath('//table:named-range').each_with_object({}) do |ne, hash|
567567
#-
568568
# $Sheet1.$C$5
569569
#+
570570
name = attribute(ne, 'name').to_s
571571
sheetname, coords = attribute(ne, 'cell-range-address').to_s.split('.$')
572572
col, row = coords.split('$')
573573
sheetname = sheetname[1..-1] if sheetname[0, 1] == '$'
574-
[name, [sheetname, row, col]]
575-
end]
574+
hash[name] = [sheetname, row, col]
575+
end
576576
end
577577

578578
def read_styles(style_elements)

0 commit comments

Comments
 (0)