0
0
Rubyprogramming~10 mins

Let and before hooks in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Let and before hooks
Define let variable
Run before hook
Execute test example
Use let variable (lazy evaluated)
Test completes
This flow shows how a let variable is defined lazily, before hooks run setup code, and then the test example uses the let variable.
Execution Sample
Ruby
let(:number) { 5 }

before do
  @result = number * 2
end

it "doubles the number" do
  expect(@result).to eq(10)
end
This code defines a let variable 'number', runs a before hook to set @result, then tests that @result equals 10.
Execution Table
StepActionEvaluationResult
1Define let(:number)No evaluation yetnumber is ready but not called
2Run before hookCalls number for first timenumber evaluated to 5, @result set to 10
3Run test exampleUse @result@result is 10, test passes
4Test endsNo more codeTest complete
💡 Test ends after example runs and assertions pass
Variable Tracker
VariableStartAfter before hookAfter test exampleFinal
number (let)not evaluatedevaluated to 5evaluated to 5evaluated to 5
@resultnil101010
Key Moments - 3 Insights
Why is the let variable 'number' not evaluated when defined?
Because let variables are lazy; they only run when first called, as shown in step 2 of the execution_table.
When does the before hook run in relation to the test example?
The before hook runs before the test example, setting up variables like @result, as seen in step 2 before step 3.
Can the let variable be used inside the before hook?
Yes, the let variable is evaluated the first time it is called, even inside the before hook, as step 2 shows.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the let variable 'number' first evaluated?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Check the 'Evaluation' column in execution_table rows for when 'number' is evaluated.
According to variable_tracker, what is the value of @result after the before hook runs?
A5
B10
Cnil
Dundefined
💡 Hint
Look at the '@result' row under 'After before hook' column in variable_tracker.
If the before hook did not call 'number', when would 'number' be evaluated?
ANever
BAt test definition time
CWhen the test example calls it
DBefore the before hook
💡 Hint
Recall that let variables are lazy and only evaluated when first used, as shown in the flow and execution_table.
Concept Snapshot
let(:name) { value } defines a lazy variable.
before hooks run setup code before each test.
let variables are evaluated only when first called.
before hooks can use let variables, triggering evaluation.
Test examples use variables set up by before hooks and let.
This helps organize test setup cleanly and efficiently.
Full Transcript
This visual execution trace shows how Ruby's let and before hooks work in tests. First, a let variable 'number' is defined but not evaluated immediately. Then, the before hook runs, calling 'number' for the first time, which evaluates it to 5 and sets @result to 10. Next, the test example runs, using @result which is 10, so the test passes. The variable tracker shows 'number' changes from not evaluated to 5 after the before hook, and @result changes from nil to 10. Key moments clarify that let variables are lazy and evaluated only when called, including inside before hooks. The quizzes check understanding of when evaluation happens and variable values. The snapshot summarizes that let defines lazy variables, before hooks run setup before tests, and together they organize test code cleanly.