Ruby Quick Reference

Quick reference for efficient coding

View the Project on GitHub YumaYX/RubyQuickReference

write_file_with_write.rb(json)

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’.

Execution:

require 'json'
#=> true

hash = {:key => 'value'}
#=> {:key=>"value"}

File.write('file.json', JSON.dump(hash))
#=> 15

Executed with Ruby 3.3.5