Bird
0
0

What will be the output of this Ruby code using the pipeline operator?

hard📝 Application Q8 of 15
Ruby - Functional Patterns in Ruby

What will be the output of this Ruby code using the pipeline operator?

def triple(x)
  x * 3
end

def decrement(x)
  x - 1
end

result = 2 |> triple |> decrement |> triple
puts result
A15
B18
C21
D24
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate 2 |> triple

    Calling triple(2) returns 2 * 3 = 6.
  2. Step 2: Evaluate 6 |> decrement

    Calling decrement(6) returns 6 - 1 = 5.
  3. Step 3: Evaluate 5 |> triple

    Calling triple(5) returns 5 * 3 = 15.
  4. Final Answer:

    15 -> Option A
  5. Quick Check:

    Calculate stepwise and verify final output [OK]
Quick Trick: Evaluate pipeline step-by-step left to right [OK]
Common Mistakes:
  • Applying methods in wrong order
  • Miscomputing intermediate results
  • Confusing decrement with decrementing by 2

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes