Skip to content

Commit 819871c

Browse files
committed
Enable Style/MapToHash cop
Ruby 2.6 added block argument processing to `Enumerable#to_h`. https://bugs.ruby-lang.org/issues/15143 Rails 7 requires Ruby 2.7.0 or higher, so the new feature can use it. `Style/MapToHash` cop will detect it. And this cop in the `Style` department, but this seems to improve performance as follows: ```ruby # map_to_hash.rb require 'benchmark/ips' ARRAY = (1..100).to_a HASH = {foo: 1, bar: 2} Benchmark.ips do |x| x.report('array.map.to_h') { ARRAY.map { |v| [v, v * 2] }.to_h } x.report('array.to_h') { ARRAY.to_h { |v| [v, v * 2] } } x.compare! end Benchmark.ips do |x| x.report('hash.map.to_h') { HASH.map { |k, v| [k.to_s, v * 2] }.to_h } x.report('hash.to_h') { HASH.to_h { |k, v| [k.to_s, v * 2] } } x.compare! end ``` ```console % ruby map_to_hash.rb Warming up -------------------------------------- array.map.to_h 9.063k i/100ms array.to_h 9.609k i/100ms Calculating ------------------------------------- array.map.to_h 89.063k (± 3.9%) i/s - 453.150k in 5.096572s array.to_h 96.449k (± 1.7%) i/s - 490.059k in 5.082529s Comparison: array.to_h: 96448.7 i/s array.map.to_h: 89063.4 i/s - 1.08x (± 0.00) slower Warming up -------------------------------------- hash.map.to_h 106.284k i/100ms hash.to_h 149.354k i/100ms Calculating ------------------------------------- hash.map.to_h 1.102M (± 2.2%) i/s - 5.527M in 5.019657s hash.to_h 1.490M (± 0.9%) i/s - 7.468M in 5.013264s Comparison: hash.to_h: 1489707.0 i/s hash.map.to_h: 1101561.5 i/s - 1.35x (± 0.00) slower ``` `Style/MapToHash` cop ... https://docs.rubocop.org/rubocop/1.25/cops_style.html#stylemaptohash
1 parent 49299c4 commit 819871c

File tree

7 files changed

+16
-13
lines changed

7 files changed

+16
-13
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ Style/FrozenStringLiteralComment:
159159
- 'actionmailbox/db/migrate/**/*.rb'
160160
- 'actiontext/db/migrate/**/*.rb'
161161

162+
Style/MapToHash:
163+
Enabled: true
164+
162165
Style/RedundantFreeze:
163166
Enabled: true
164167

actiontext/lib/action_text/attachment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def inspect
9191

9292
private
9393
def node_attributes
94-
@node_attributes ||= ATTRIBUTES.map { |name| [ name.underscore, node[name] ] }.to_h.compact
94+
@node_attributes ||= ATTRIBUTES.to_h { |name| [ name.underscore, node[name] ] }.compact
9595
end
9696

9797
def attachable_attributes

actiontext/lib/action_text/trix_attachment.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ def transform_attribute_keys(attributes)
3939
end
4040

4141
def typecast_attribute_values(attributes)
42-
attributes.map do |key, value|
42+
attributes.to_h do |key, value|
4343
typecast = ATTRIBUTE_TYPES[key] || ATTRIBUTE_TYPES[:default]
4444
[key, typecast.call(value)]
45-
end.to_h
45+
end
4646
end
4747
end
4848

actionview/lib/action_view/ripper_ast_parser.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ def to_hash
8686
end
8787

8888
def hash_from_body(body)
89-
body.map do |hash_node|
89+
body.to_h do |hash_node|
9090
return nil if hash_node.type != :assoc_new
9191

9292
[hash_node[0], hash_node[1]]
93-
end.to_h
93+
end
9494
end
9595

9696
def symbol?
@@ -189,9 +189,9 @@ def parse_render_nodes(code)
189189
parser = RenderCallExtractor.new(code)
190190
parser.parse
191191

192-
parser.render_calls.group_by(&:first).collect do |method, nodes|
192+
parser.render_calls.group_by(&:first).to_h do |method, nodes|
193193
[ method.to_sym, nodes.collect { |v| v[1] } ]
194-
end.to_h
194+
end
195195
end
196196
end
197197
end

activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def indexes(table_name)
5757
orders = options.delete(:orders)
5858
lengths = options.delete(:lengths)
5959

60-
columns = index[-1].map { |name|
60+
columns = index[-1].to_h { |name|
6161
[ name.to_sym, expressions[name] || +quote_column_name(name) ]
62-
}.to_h
62+
}
6363

6464
index[-1] = add_options_for_index_columns(
6565
columns, order: orders, length: lengths

activerecord/lib/active_record/encryption/encryptable_record.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,12 @@ def build_encrypt_attribute_assignments
188188
end
189189

190190
def build_decrypt_attribute_assignments
191-
Array(self.class.encrypted_attributes).collect do |attribute_name|
191+
Array(self.class.encrypted_attributes).to_h do |attribute_name|
192192
type = type_for_attribute(attribute_name)
193193
encrypted_value = ciphertext_for(attribute_name)
194194
new_value = type.deserialize(encrypted_value)
195195
[attribute_name, new_value]
196-
end.to_h
196+
end
197197
end
198198

199199
def cant_modify_encrypted_attributes_when_frozen

activestorage/test/service/mirror_service_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
require "service/shared_service_tests"
44

55
class ActiveStorage::Service::MirrorServiceTest < ActiveSupport::TestCase
6-
mirror_config = (1..3).map do |i|
6+
mirror_config = (1..3).to_h do |i|
77
[ "mirror_#{i}",
88
service: "Disk",
99
root: Dir.mktmpdir("active_storage_tests_mirror_#{i}") ]
10-
end.to_h
10+
end
1111

1212
config = mirror_config.merge \
1313
mirror: { service: "Mirror", primary: "primary", mirrors: mirror_config.keys },

0 commit comments

Comments
 (0)