Bird
0
0

What will be printed by this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Functional Patterns in Ruby

What will be printed by this Ruby code?

def to_str(x)
  "Number: #{x}"
end

result = 5 |> to_str
puts result
AError: undefined method
B5
CNumber: x
D"Number: 5"
Step-by-Step Solution
Solution:
  1. Step 1: Pass 5 to to_str method

    The pipeline operator sends 5 as argument to to_str, which returns the string "Number: 5".
  2. Step 2: Print the returned string

    puts outputs the string "Number: 5" exactly.
  3. Final Answer:

    "Number: 5" -> Option D
  4. Quick Check:

    Pipeline passes 5 to to_str = "Number: 5" [OK]
Quick Trick: Pipeline passes value as argument, so string interpolation works [OK]
Common Mistakes:
  • Expecting output to be just 5
  • Confusing variable name with string literal

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes