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

Lint/NestedMethodDefinition shouldn't flag methods defined on Structs #3048

Closed
emschwar opened this issue Apr 14, 2016 · 0 comments
Closed

Comments

@emschwar
Copy link

I have some code that generates a Struct that has some methods added to it:

def listener
  @listener ||= Struct.new(:thing, :to, :listen_to) do
    def all_the_things
      "#{thing} #{to} #{listen_to}"
    end
  end
end

According to http://ruby-doc.org/core-2.2.0/Struct.html, this is the recommended way to customize a struct, but this code fails the cop:

$ bundle exec rubocop -D test.rb
Inspecting 1 file
W

Offenses:

test.rb:3:5: W: Lint/NestedMethodDefinition: Method definitions must not be nested. Use lambda instead.
    def all_the_things ...
    ^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

Steps to reproduce the problem

The method above should trigger the cop.

RuboCop version

Include the output of rubocop -V:

$ rubocop -V
0.39.0 (using Parser 2.3.0.7, running on ruby 2.2.4 x86_64-linux-musl)
bbatsov added a commit that referenced this issue Apr 22, 2016
…_as_nested

[Fix #3048] Do not report defs in Struct.new block as nested
Neodelf pushed a commit to Neodelf/rubocop that referenced this issue Oct 15, 2016
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