Bird
0
0

What is wrong with this Ruby code snippet?

medium📝 Debug Q6 of 15
Ruby - Enumerable and Collection Processing
What is wrong with this Ruby code snippet?
values = [4, 5, 6]
values.each do value
  puts value + 1
end
AThe array should be defined with curly braces instead of square brackets
BThe block parameter should be enclosed in vertical bars (| |)
CThe <code>puts</code> method cannot be used inside an <code>each</code> block
DThe <code>each</code> method requires parentheses around the block
Step-by-Step Solution
Solution:
  1. Step 1: Review the block syntax for each

    In Ruby, block parameters must be enclosed within vertical bars, e.g., do |value|.
  2. Step 2: Identify the error

    The code uses do value without vertical bars, which is a syntax error.
  3. Final Answer:

    The block parameter should be enclosed in vertical bars (| |) -> Option B
  4. Quick Check:

    Block parameters need | | [OK]
Quick Trick: Always use | | around block parameters in each [OK]
Common Mistakes:
  • Omitting vertical bars around block variables
  • Using wrong brackets for arrays
  • Misunderstanding block syntax requirements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes