Bird
0
0

Which of the following is the correct syntax to check if a block was given inside a Ruby method?

easy📝 Syntax Q12 of 15
Ruby - Blocks, Procs, and Lambdas
Which of the following is the correct syntax to check if a block was given inside a Ruby method?
Aif block_given then yield end
Bif block_given? () yield end
Cif block_given? then yield end
Dif block_given? yield () end
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct method call syntax

    block_given? is a method and should be called with a question mark and optionally parentheses. The most common and clear syntax is block_given? without parentheses.
  2. Step 2: Confirm correct use of yield

    The yield keyword runs the block if it exists. The syntax if block_given? then yield end is correct and clear.
  3. Final Answer:

    if block_given? then yield end -> Option C
  4. Quick Check:

    Correct syntax uses block_given? and yield [OK]
Quick Trick: Use block_given? with question mark and yield inside if [OK]
Common Mistakes:
  • Omitting question mark in block_given?
  • Using block_given without parentheses incorrectly
  • Calling yield with parentheses unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes