Last active
May 21, 2023 15:44
-
-
Save sephraim/4f6262ab5ab9d1aa3086211143b3b28d to your computer and use it in GitHub Desktop.
[Read / write YAML file]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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