Skip to content

[Doc] Add a lambda good example to Rake/MethodDefinitionInTask - #63

Merged
koic merged 1 commit into
rubocop:masterfrom
corsonknowles:docs-lambda-good-example-method-definition-in-task
Aug 22, 2026
Merged

[Doc] Add a lambda good example to Rake/MethodDefinitionInTask#63
koic merged 1 commit into
rubocop:masterfrom
corsonknowles:docs-lambda-good-example-method-definition-in-task

Conversation

@corsonknowles

@corsonknowles corsonknowles commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a second # good example to the Rake/MethodDefinitionInTask documentation, showing a lambda assigned to a task-local variable as an alternative to hoisting the def to the top level.

The cop's full description today reads:

Detects method definition in a task or namespace, because it is defined to the top level. It is confusing because the scope looks in the task or namespace, but actually it is defined to the top level.

Its only # good example moves the def above the task. That's a valid fix, but not always the one you want: a top-level def becomes a method on Object, so it is visible everywhere and can silently collide with a same-named helper in another rake file (all .rake files load into a single process — last definition wins). A lambda bound to a local avoids both problems.

New example

# good - A lambda assigned to a local variable is scoped to the
#        block, so it is not defined at the top level.
task :foo do
  helper = -> { do_something }
  helper.call
end

Why this is genuinely "good" (and the closure nuance)

A def inside a task/namespace block attaches to the current definee, which at a rake file's top level is Object — blocks don't change the definee, which is exactly the confusion the cop names. Assigning helper = -> { ... } is a local variable assignment, which is lexically scoped to the block and never touches Object.

The nuance worth noting: the lambda's body still closes over the block's bindings, so it can freely use the task's locals and arguments — it is a closure over the task scope. But the name is a block-local, not a top-level method, so nothing leaks. A lambda's return also returns from the lambda (matching method semantics), so helpers that use early returns keep working.

Docs-only change; bundle exec rake (rubocop + spec) is green.

corsonknowles added a commit to corsonknowles/rubocop-rake that referenced this pull request Jul 1, 2026
@corsonknowles
corsonknowles force-pushed the docs-lambda-good-example-method-definition-in-task branch from 8ae5a59 to bc5b2d3 Compare August 10, 2026 11:47
@koic

koic commented Aug 17, 2026

Copy link
Copy Markdown
Member

Documentation-only changes don't need a changelog entry.

The only good example moves the def to the top level, which pollutes Object
and can collide with same-named helpers in sibling rake files (all .rake files
load into one process). Document assigning a lambda to a task-local as an
alternative: the local is scoped to the block, so nothing is defined at the
top level.
@corsonknowles
corsonknowles force-pushed the docs-lambda-good-example-method-definition-in-task branch from bc5b2d3 to 15b27eb Compare August 20, 2026 02:41
@corsonknowles

Copy link
Copy Markdown
Contributor Author

Thanks @koic
Amended!

@koic
koic merged commit 890aabc into rubocop:master Aug 22, 2026
9 checks passed
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

Successfully merging this pull request may close these issues.

2 participants