Summary
Make hook invocation compatible with both zero-arity and one-arity blocks by not always passing an argument to instance_exec.
Motivation
register_hooks currently invokes all hook blocks with one argument (example):
instance_exec(example, &hook[:block])
This is compatible with proc blocks that ignore args, but zero-arity lambdas raise ArgumentError when given one argument.
Concrete example
YardExampleTest.before(&-> { @flag = true })
Current behavior fails with:
ArgumentError: wrong number of arguments (given 1, expected 0)
Current behaviour
All hooks are called as if they accept one argument, regardless of their declared arity.
Proposed solution
Dispatch based on callable arity:
- If hook arity is
0, call instance_exec(&hook[:block])
- If hook arity is
1 (or variable arity), call instance_exec(example, &hook[:block])
This preserves the current "optional example argument" behavior while supporting strict lambdas.
Scope
In scope
- Update hook invocation in
register_hooks to handle lambda/proc arity safely
- Add tests for zero-arity lambda hooks and one-arity hooks
- Preserve existing behavior for global and test-scoped hooks
Out of scope
- Changing hook registration API shape
- Introducing multi-argument hook payloads
Implementation notes
Proc#lambda? may be used if you want strict handling only for lambdas; arity-based dispatch is usually sufficient.
- Keep execution context unchanged (
instance_exec context remains the example spec instance).
- Ensure both
before and after hooks share identical invocation behavior.
Acceptance criteria
Related
lib/yard_example_test/example.rb — register_hooks
lib/yard_example_test.rb — before / after
Summary
Make hook invocation compatible with both zero-arity and one-arity blocks by not always passing an argument to
instance_exec.Motivation
register_hookscurrently invokes all hook blocks with one argument (example):This is compatible with proc blocks that ignore args, but zero-arity lambdas raise
ArgumentErrorwhen given one argument.Concrete example
Current behavior fails with:
Current behaviour
All hooks are called as if they accept one argument, regardless of their declared arity.
Proposed solution
Dispatch based on callable arity:
0, callinstance_exec(&hook[:block])1(or variable arity), callinstance_exec(example, &hook[:block])This preserves the current "optional example argument" behavior while supporting strict lambdas.
Scope
In scope
register_hooksto handle lambda/proc arity safelyOut of scope
Implementation notes
Proc#lambda?may be used if you want strict handling only for lambdas; arity-based dispatch is usually sufficient.instance_execcontext remains the example spec instance).beforeandafterhooks share identical invocation behavior.Acceptance criteria
ArgumentErrorRelated
lib/yard_example_test/example.rb—register_hookslib/yard_example_test.rb—before/after