class RuboCop::Cop::Style::SingleArgumentDig
{ key1: { key2: ‘value’ } }.dig(*keys)
keys = %i[key1 key2]
# good
[1, [2, [3]]].dig(1, 1)
{ key1: { key2: ‘value’ } }.dig(:key1, :key2)
# good
[1, 2, 3][0]
{ key: ‘value’ }[:key]
# good
[1, 2, 3].dig(0)
{ key: ‘value’ }.dig(:key)
# bad
@example
of ‘dig`.
is an `Enumerable` or does not have a nonstandard implementation
This cop is unsafe because it cannot be guaranteed that the receiver
@safety
argument. In such cases, dig should be replaced with [].
Sometimes using dig method ends up with just a single
def on_send(node)
def on_send(node) return unless node.receiver expression = single_argument_dig?(node) return unless expression return if expression.forwarded_args_type? receiver = node.receiver.source argument = expression.source message = format(MSG, receiver: receiver, argument: argument, original: node.source) add_offense(node, message: message) do |corrector| next if part_of_ignored_node?(node) correct_access = "#{receiver}[#{argument}]" corrector.replace(node, correct_access) end ignore_node(node) end