class RuboCop::Cop::Lint::NestedMethodDefinition
end
end
end
def bar
class << self
def foo
# good
@example
end
end
end
def bar
self.class.module_exec do
def foo
end
end
end
def bar
self.class.class_eval do
def foo
# good
@example
end
bar.call
bar = -> { puts ‘hello’ }
def foo
# good
@example
end
end
def bar
def foo
# will be redefined every time ‘foo` is invoked.
# as the outer `foo` method. Furthermore, the `bar` method
# `bar` definition actually produces methods in the same scope
# bad
@example
This cop checks for nested method definitions.
def on_def(node)
def on_def(node) subject, = *node return if node.defs_type? && subject.lvar_type? def_ancestor = node.each_ancestor(:def, :defs).first return unless def_ancestor within_scoping_def = node.each_ancestor(:block, :sclass).any? do |ancestor| scoping_method_call?(ancestor) end add_offense(node) if def_ancestor && !within_scoping_def end
def scoping_method_call?(child)
def scoping_method_call?(child) child.sclass_type? || eval_call?(child) || exec_call?(child) || class_or_module_or_struct_new_call?(child) end