Skip to content

Instantly share code, notes, and snippets.

@sephraim
Last active May 21, 2023 15:44
Show Gist options
  • Save sephraim/4f6262ab5ab9d1aa3086211143b3b28d to your computer and use it in GitHub Desktop.
Save sephraim/4f6262ab5ab9d1aa3086211143b3b28d to your computer and use it in GitHub Desktop.
[Read / write YAML file]
require 'json'
require 'openstruct'
require 'yaml'
FILE_PATH = '/path/to/file.yml'
### LOAD YAML FILE AS A YAML OBJECT ###
yaml = YAML.load_file(FILE_PATH) # with string keys
yaml = JSON.parse(YAML.load_file(FILE_PATH).to_json, symbolize_names: true) # with symbol keys
### LOAD YAML FILE AS AN OPENSTRUCT ###
yaml = JSON.parse(YAML.load_file(FILE_PATH).to_json, object_class: OpenStruct)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment