Bird
0
0

Identify the error in the following Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Hashes
Identify the error in the following Ruby code snippet:
h = {x: 10, y: 20}
h.each do |k v|
  puts "#{k}: #{v}"
end
AMissing comma between block parameters <code>k</code> and <code>v</code>
BHash keys must be strings, not symbols
CThe <code>each</code> method cannot be used with <code>do-end</code>
DThe <code>puts</code> statement syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check block parameter syntax

    Block parameters must be separated by a comma inside pipes: |k, v|. Here comma is missing.
  2. Step 2: Verify other parts

    Keys as symbols are valid, each do-end is valid, and puts syntax is correct.
  3. Final Answer:

    Missing comma between block parameters k and v -> Option A
  4. Quick Check:

    Block params need comma [OK]
Quick Trick: Always separate block parameters with commas inside pipes [OK]
Common Mistakes:
  • Omitting comma between block variables
  • Thinking symbols are invalid keys
  • Confusing block syntax with method call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes