Ruby Quick Reference

Quick reference for efficient coding

View the Project on GitHub YumaYX/RubyQuickReference

read_with_load.rb(json)

This Ruby code snippet uses the ‘json’ library to load and parse data from a JSON file named ‘file.json’. The ‘require’ statement is used to include the necessary JSON library, and ‘JSON.load’ is then employed to read and convert the contents of the JSON file into a Ruby data structure.

Execution:

require 'json'
#=> true
JSON.load(File.read('file.json'))
#=> {"key"=>"value"}

Executed with Ruby 3.3.5