[Doc] Add a lambda good example to Rake/MethodDefinitionInTask - #63
Merged
koic merged 1 commit intoAug 22, 2026
Conversation
corsonknowles
added a commit
to corsonknowles/rubocop-rake
that referenced
this pull request
Jul 1, 2026
corsonknowles
force-pushed
the
docs-lambda-good-example-method-definition-in-task
branch
from
August 10, 2026 11:47
8ae5a59 to
bc5b2d3
Compare
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
force-pushed
the
docs-lambda-good-example-method-definition-in-task
branch
from
August 20, 2026 02:41
bc5b2d3 to
15b27eb
Compare
Contributor
Author
|
Thanks @koic |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a second
# goodexample to theRake/MethodDefinitionInTaskdocumentation, showing a lambda assigned to a task-local variable as an alternative to hoisting thedefto the top level.The cop's full description today reads:
Its only
# goodexample moves thedefabove the task. That's a valid fix, but not always the one you want: a top-leveldefbecomes a method onObject, so it is visible everywhere and can silently collide with a same-named helper in another rake file (all.rakefiles load into a single process — last definition wins). A lambda bound to a local avoids both problems.New example
Why this is genuinely "good" (and the closure nuance)
A
definside atask/namespaceblock attaches to the current definee, which at a rake file's top level isObject— blocks don't change the definee, which is exactly the confusion the cop names. Assigninghelper = -> { ... }is a local variable assignment, which is lexically scoped to the block and never touchesObject.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
returnalso 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.