Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Blocks, Procs, and Lambdas
What will be the output of the following Ruby code?
def greet
  yield("Alice")
end

greet { |name| puts "Hello, #{name}!" }
AError: no block given
BHello, Alice!
CHello, !
DHello, name!
Step-by-Step Solution
Solution:
  1. Step 1: Understand yield with argument

    The method greet calls yield with the string "Alice" as argument, passing it to the block.
  2. Step 2: Evaluate the block execution

    The block receives "Alice" as name and prints "Hello, Alice!".
  3. Final Answer:

    Hello, Alice! -> Option B
  4. Quick Check:

    Yield passes "Alice" to block = C [OK]
Quick Trick: Yield passes arguments to block parameters [OK]
Common Mistakes:
  • Ignoring the argument passed to yield
  • Assuming block is not called
  • Confusing variable names in block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes