Skip to content

Hooks: support zero-arity lambdas in before/after callbacks #5

Description

@jcouball

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

  1. Proc#lambda? may be used if you want strict handling only for lambdas; arity-based dispatch is usually sufficient.
  2. Keep execution context unchanged (instance_exec context remains the example spec instance).
  3. Ensure both before and after hooks share identical invocation behavior.

Acceptance criteria

  • Zero-arity lambda hooks run without ArgumentError
  • One-arity hooks still receive the example argument
  • Existing hook scenarios continue to pass

Related

  • lib/yard_example_test/example.rbregister_hooks
  • lib/yard_example_test.rbbefore / after

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions