class RuboCop::Cop::Style::StringHashKeys
{ one: 1, two: 2, three: 3 }
# good
{ ‘one’ => 1, ‘two’ => 2, ‘three’ => 3 }
# bad
@example
there are instances when string keys are required.
This cop is unsafe because while symbols are preferred for hash keys,
@safety
symbols is preferred instead.
Checks for the use of strings as keys in hashes. The use of
def on_pair(node)
def on_pair(node) return unless string_hash_key?(node) key_content = node.key.str_content return unless key_content.valid_encoding? return if receive_environments_method?(node) add_offense(node.key) do |corrector| symbol_content = key_content.to_sym.inspect corrector.replace(node.key, symbol_content) end end