Bird
0
0

What is wrong with this Ruby code?

medium📝 Debug Q7 of 15
Ruby - File IO

What is wrong with this Ruby code?

require 'json'
hash = {name: "Eve", age: 28}
json_str = JSON.parse(hash)
AJSON.parse should be JSON.generate here
BJSON.parse expects a JSON string, not a Ruby hash
CHash keys must be strings, not symbols
DMissing require 'json' statement
Step-by-Step Solution
Solution:
  1. Step 1: Understand JSON.parse input requirements

    JSON.parse expects a JSON formatted string as input, not a Ruby hash.
  2. Step 2: Identify correct method to convert hash to JSON string

    To convert a Ruby hash to JSON string, use JSON.generate, not JSON.parse.
  3. Final Answer:

    JSON.parse expects a JSON string, not a Ruby hash -> Option B
  4. Quick Check:

    JSON.parse input = JSON string, not Ruby object [OK]
Quick Trick: Use JSON.generate for Ruby object to JSON string conversion [OK]
Common Mistakes:
  • Passing Ruby hash to JSON.parse
  • Confusing JSON.parse and JSON.generate
  • Using symbol keys incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes