0
0
Rubyprogramming~10 mins

Pipeline operator concept in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pipeline operator concept
Start with initial value
Apply first function
Pass result to next function
Apply next function
Final result after all functions
The pipeline operator passes the result of one expression as input to the next, chaining operations step-by-step.
Execution Sample
Ruby
result = 5 |> double |> increment

def double(x)
  x * 2
end

def increment(x)
  x + 1
end
This code doubles 5, then increments the result by 1 using the pipeline operator.
Execution Table
StepExpressionInputOperationOutput
15 |> double5Call double(5)10
210 |> increment10Call increment(10)11
3Final result11No more operations11
💡 All pipeline operations completed, final result is 11
Variable Tracker
VariableStartAfter Step 1After Step 2Final
resultundefined101111
Key Moments - 2 Insights
Why does the output of double become the input for increment?
Because the pipeline operator |> passes the output of the left expression as the argument to the function on the right, as shown in execution_table steps 1 and 2.
What happens if a function in the pipeline does not accept an argument?
The pipeline operator expects the right side to be a function that takes the left side's output as input. If it doesn't, Ruby will raise an error. This is why each function must accept one argument, as seen in the sample code.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 1?
A10
B5
C11
Dundefined
💡 Hint
Check the 'Output' column for step 1 in the execution_table.
At which step does the pipeline operator pass the value 10 as input?
AStep 1
BStep 3
CStep 2
DNo step
💡 Hint
Look at the 'Input' column in execution_table for step 2.
If the function increment added 2 instead of 1, what would be the final result?
A11
B12
C10
D13
💡 Hint
Add 2 to the output of double (10) instead of 1, as shown in variable_tracker.
Concept Snapshot
Pipeline operator |> chains functions by passing the output of one as input to the next.
Syntax: value |> function1 |> function2
Each function must accept one argument.
This creates clear, readable sequences of operations.
Final result is the output of the last function.
Full Transcript
The pipeline operator in Ruby takes the result of one expression and passes it as an argument to the next function. In the example, starting with 5, the double function is called, returning 10. Then 10 is passed to increment, returning 11. This chaining makes code easier to read and write by avoiding nested calls. Each step shows the input, operation, and output clearly. Variables update after each step, with the final result stored in 'result'. Understanding this flow helps avoid errors and write clean code.