Bird
0
0

Which of the following is the correct syntax to use the times method with a block variable?

easy📝 Syntax Q3 of 15
Ruby - Loops and Iteration
Which of the following is the correct syntax to use the times method with a block variable?
A5.times do |i| puts i end
B5.times { |i puts i }
C5.times { puts |i| i }
D5.times |i| { puts i }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct block syntax with variable

    Ruby allows do |var| ... end or { |var| ... } for blocks.
  2. Step 2: Check each option's syntax correctness

    5.times do |i| puts i end uses do |i| ... end correctly. 5.times { |i puts i } misses the closing pipe around the block variable. Options A, B, and D have syntax errors.
  3. Final Answer:

    5.times do |i| puts i end -> Option A
  4. Quick Check:

    Block variable syntax = do |var| ... end [OK]
Quick Trick: Use do |var| ... end or { |var| ... } correctly [OK]
Common Mistakes:
MISTAKES
  • Placing block variable outside braces
  • Using pipes incorrectly inside block
  • Mixing block syntax styles

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes