class RuboCop::Cop::Style::SendWithLiteralMethodName
obj.method_name
# good
obj.__send__(‘method_name’)
obj.__send__(:method_name)
obj.send(‘method_name’)
obj.send(:method_name)
# bad
@example AllowSend: false
obj.__send__(‘method_name’)
obj.__send__(:method_name)
obj.send(‘method_name’)
obj.send(:method_name)
# good
@example AllowSend: true (default)
obj.method_name
# good
obj.public_send(‘method_name’)
obj.public_send(:method_name)
# bad
@example
the ‘send` method being detected is calling a private method.
Additionally, when `AllowSend` is set to `true`, it cannot determine whether
This cop is not safe because it can incorrectly detect based on the receiver.
@safety
—-
send(:foo=, 1) # => 42
self.foo = 1 # => 1
end
42
@foo = foo
def foo=(foo)
—-
[source,ruby]
behavior differs as follows:
NOTE: Writer methods with names ending in `=` are always permitted because their
only the `public_send` method is detected.
Since the `send` method can be used to call private methods, by default,
Detects the use of the `public_send` method with a literal method name argument.
def allow_send?
def allow_send? !!cop_config['AllowSend'] end
def offense_range(node)
def offense_range(node) node.loc.selector.join(node.source_range.end) end
def on_send(node)
def on_send(node) return if allow_send? && !node.method?(:public_send) return unless (first_argument = node.first_argument) return unless first_argument.type?(*STATIC_METHOD_NAME_NODE_TYPES) offense_range = offense_range(node) method_name = first_argument.value return if !METHOD_NAME_PATTERN.match?(method_name) || RESERVED_WORDS.include?(method_name) add_offense(offense_range, message: format(MSG, method_name: method_name)) do |corrector| if node.arguments.one? corrector.replace(offense_range, method_name) else corrector.replace(node.loc.selector, method_name) corrector.remove(removal_argument_range(first_argument, node.arguments[1])) end end end
def removal_argument_range(first_argument, second_argument)
def removal_argument_range(first_argument, second_argument) first_argument.source_range.begin.join(second_argument.source_range.begin) end