Quick reference for efficient coding
This Ruby code snippet uses the ‘json’ library to work with JSON data. It creates a hash with a key-value pair and then writes the hash as a JSON-formatted string to a file named ‘file.json’.
require 'json'
#=> true
hash = {:key => 'value'}
#=> {:key=>"value"}
File.write('file.json', JSON.dump(hash))
#=> 15
Executed with Ruby 3.3.5