Ruby Quick Reference

Quick reference for efficient coding

View the Project on GitHub YumaYX/RubyQuickReference

read_with_open.rb(json)

This Ruby code uses the ‘json’ library to read and parse a JSON file named ‘file.json’. It opens the file and uses the JSON.load method to load and parse the content, returning a Ruby data structure representing the JSON data.

Execution:

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

Executed with Ruby 3.3.5