Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested method false positive with Class.new #2707

Closed
owst opened this issue Jan 23, 2016 · 0 comments
Closed

Nested method false positive with Class.new #2707

owst opened this issue Jan 23, 2016 · 0 comments

Comments

@owst
Copy link
Contributor

owst commented Jan 23, 2016

The following code dynamically creates a class using Class.new:

class NestedMethods
  def definer
    Class.new do
      def foo
        'foo'
      end
    end
  end
end

and rubocop (0.36.0 (using Parser 2.3.0.1, running on ruby 2.2.0 x86_64-darwin14)) reports that foo is nested:

foo.rb:4:7: W: Method definitions must not be nested. Use lambda instead.
      def foo
      ^^^^^^^

However, these method definitions are not nested, since Class.new has changed the internal klass pointer and foo is defined on the new class rather than NestedMethods. Furthermore, as the docs say: If a block is given, it is passed the class object, and the block is evaluated in the context of this class using class_eval., therefore I believe the nested method warning should treat Class.new in the same way as class_eval and instance_eval, namely, not issuing a nested method definition warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant