Iterate over a .yml file's contents

Public, Rails General, Ruby General

harrylevine

Created: Jul 01, 2017     Updated: Jul 01, 2017


Let's assume that the .yml file (i.e. code_school.yml) is located in this directory:

config/code_schools.yml

You'll grab the path to the file, and load its contents:

config = Rails.root + 'config/code_schools.yml'
code_schools = YAML.load_file(config)

Then you have an Array of Hashes that you can iterate over:

code_schools.class 
=> Array

code_schools.first
=> {"name"=>"Wyncode Academy", "url"=>"http://wyncode.co", "hardware_included"=>false, "has_online"=>false, "online_only"=>false, "locations"=>[{"va_accepted"=>true, "address1"=>"549 SW 28th Street", "city"=>"Miami", "state"=>"FL", "zip"=>33127}]}