Bird
0
0

What will be the output of this Ruby code if the gem 'json' is installed?

medium📝 Predict Output Q13 of 15
Ruby - Gems and Bundler
What will be the output of this Ruby code if the gem 'json' is installed?
require 'json'
puts JSON.generate({name: 'Ruby', type: 'language'})
ASyntaxError
B{name: Ruby, type: language}
C{"name":"Ruby","type":"language"}
Dnil
Step-by-Step Solution
Solution:
  1. Step 1: Understand the require statement

    The code requires the 'json' gem, which provides JSON methods.
  2. Step 2: Analyze JSON.generate output

    JSON.generate converts the Ruby hash to a JSON string with keys and values in quotes.
  3. Final Answer:

    {"name":"Ruby","type":"language"} -> Option C
  4. Quick Check:

    JSON.generate outputs JSON string [OK]
Quick Trick: JSON.generate outputs string with quotes [OK]
Common Mistakes:
  • Expecting Ruby hash syntax as output
  • Confusing JSON.generate with JSON.parse
  • Ignoring quotes around keys and values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes