Recall & Review
beginner
What is the purpose of the JSON library in Ruby?
The JSON library in Ruby is used to convert Ruby objects into JSON strings and parse JSON strings back into Ruby objects.
Click to reveal answer
beginner
How do you convert a Ruby hash to a JSON string?
Use
JSON.generate(your_hash) or your_hash.to_json to convert a Ruby hash into a JSON string.Click to reveal answer
beginner
How do you parse a JSON string into a Ruby object?
Use
JSON.parse(json_string) to convert a JSON string into a Ruby hash or array.Click to reveal answer
beginner
What Ruby module must you require to use JSON methods?
You must include
require 'json' at the top of your Ruby file to use JSON methods.Click to reveal answer
intermediate
What is the difference between
JSON.generate and JSON.dump?JSON.generate returns a JSON string from a Ruby object. JSON.dump does the same but can also write the JSON string directly to an IO object like a file.Click to reveal answer
Which method converts a Ruby hash to a JSON string?
✗ Incorrect
JSON.generate converts Ruby objects to JSON strings. JSON.parse does the opposite.
What must you do before using JSON methods in Ruby?
✗ Incorrect
You must require 'json' to load the JSON library before using its methods.
Which method parses a JSON string into a Ruby object?
✗ Incorrect
JSON.parse converts JSON strings into Ruby hashes or arrays.
What does
JSON.dump do that JSON.generate does not?✗ Incorrect
JSON.dump can write JSON strings directly to files or other IO objects.
If you have a Ruby array, which method converts it to JSON?
✗ Incorrect
JSON.generate works with arrays and hashes to create JSON strings.
Explain how to convert a Ruby hash to a JSON string and then back to a Ruby object.
Think about the methods to serialize and deserialize JSON.
You got /4 concepts.
Describe the difference between JSON.generate and JSON.dump in Ruby.
Consider where the JSON output goes.
You got /3 concepts.