Bird
0
0

Which of the following is the correct way to convert a Ruby hash {"name" => "Alice"} to a JSON string?

easy📝 Syntax Q12 of 15
Ruby - File IO

Which of the following is the correct way to convert a Ruby hash {"name" => "Alice"} to a JSON string?

AJSON.generate({"name" => "Alice"})
BJSON.dump({"name" => "Alice"}, $stdout)
CJSON.load({"name" => "Alice"})
DJSON.parse({"name" => "Alice"})
Step-by-Step Solution
Solution:
  1. Step 1: Identify method to convert Ruby object to JSON string

    JSON.generate converts Ruby objects like hashes into JSON strings.
  2. Step 2: Check other methods

    JSON.parse reads JSON strings, JSON.load is similar to parse but less common, JSON.dump also converts Ruby objects but requires a string or IO target.
  3. Final Answer:

    JSON.generate({"name" => "Alice"}) -> Option A
  4. Quick Check:

    generate = Ruby object to JSON string [OK]
Quick Trick: Use generate to make JSON strings from Ruby hashes [OK]
Common Mistakes:
  • Using parse instead of generate
  • Confusing dump with parse
  • Passing Ruby hash directly to parse

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes