class RuboCop::Cop::Style::MissingRespondToMissing
end
end
super
else
@delegate.send(name, *args)
if @delegate.respond_to?(name)
def method_missing(name, *args)
end
@delegate.respond_to?(name) || super
def respond_to_missing?(name, include_private)
# good
end
end
super
else
@delegate.send(name, *args)
if @delegate.respond_to?(name)
def method_missing(name, *args)
# bad
@example
—-
delegator.upcase # => FOO
# But you can call it.
delegator.respond_to?(:upcase) # => false
# Claims to not respond to ‘upcase`.
delegator = StringDelegator.new(“foo”)
end
end
@string.send(name, *args)
def method_missing(name, *args)
end
@string = string
def initialize(string)
class StringDelegator
—-
[source,ruby]
methods like `respond_to?` to behave unexpectedly:
Not defining `respond_to_missing?` will cause metaprogramming
defining `respond_to_missing?`.
Checks for the presence of `method_missing` without also
def implements_respond_to_missing?(node)
def implements_respond_to_missing?(node) return false unless (grand_parent = node.parent.parent) grand_parent.each_descendant(node.type) do |descendant| return true if descendant.method?(:respond_to_missing?) child = descendant.children.first return true if child.respond_to?(:method?) && child.method?(:respond_to_missing?) end false end
def on_def(node)
def on_def(node) return unless node.method?(:method_missing) return if implements_respond_to_missing?(node) add_offense(node) end