Class: RuboCop::Cop::GitHub::AvoidObjectSendWithDynamicMethod
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::GitHub::AvoidObjectSendWithDynamicMethod
- Defined in:
- lib/rubocop/cop/github/avoid_object_send_with_dynamic_method.rb
Overview
Public: A Rubocop to discourage using methods like Object#send that allow you to dynamically call other methods on a Ruby object, when the method being called is itself completely dynamic. Instead, explicitly call methods by name.
Examples:
# bad
foo.send(some_variable)
# good
case some_variable
when "bar"
foo.
else
foo.baz
end
# fine
foo.send(:bar)
foo.public_send("some_method")
foo.__send__("some_#{variable}_method")
Constant Summary collapse
- MESSAGE_TEMPLATE =
"Avoid using Object#%s with a dynamic method name."- SEND_METHODS =
%i(send public_send __send__).freeze
- CONSTANT_TYPES =
%i(sym str const).freeze
Instance Method Summary collapse
Instance Method Details
#on_send(node) ⇒ Object
34 35 36 37 38 |
# File 'lib/rubocop/cop/github/avoid_object_send_with_dynamic_method.rb', line 34 def on_send(node) return unless send_method?(node) return if method_being_sent_is_constrained?(node) add_offense(source_range_for_method_call(node), message: MESSAGE_TEMPLATE % node.method_name) end |