How do I parse a YAML file in Ruby?

ruby yaml load_file example
ruby yaml example
yaml load file ruby
ruby parse yaml string
ruby parse yaml into hash
ruby yaml gem
ruby write yaml
ruby yaml to object

I would like to know how to parse a YAML file with the following contents:

--- 
javascripts: 
- fo_global:
  - lazyload-min
  - holla-min

Currently I am trying to parse it this way:

@custom_asset_packages_yml = (File.exists?("#{RAILS_ROOT}/config/asset_packages.yml") ? YAML.load_file("#{RAILS_ROOT}/config/asset_packages.yml") : nil)
    if !@custom_asset_packages_yml.nil?
      @custom_asset_packages_yml['javascripts'].each{ |js|
        js['fo_global'].each{ |script|
         script
        }
      }
    end

But it doesn't seem to work and gives me an error that the value is nil.

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

If I try this, it puts out the entire string (fo_globallazyload-minholla-min):

if !@custom_asset_packages_yml.nil?
          @custom_asset_packages_yml['javascripts'].each{ |js|
            js['fo_global']
          }
        end

Maybe I'm missing something, but why try to parse the file? Why not just load the YAML and examine the object(s) that result?

If your sample YAML is in some.yml, then this:

require 'yaml'
thing = YAML.load_file('some.yml')
puts thing.inspect

gives me

{"javascripts"=>[{"fo_global"=>["lazyload-min", "holla-min"]}]}

Saving/Retrieving Data With a YAML file in Ruby: The Basics, Writing and reading yaml files can be very simple. Here are a few examples.. “ Saving/Retrieving Data With a YAML file in Ruby: The Basics” is� I have a YAML file with a few entries that look like this: 001: :title: Some title :description: Some body text maybe 002: :title: Some title :description: Some body text maybe I'm using the following Ruby method to parse that YAML file into a set of objects I can iterate over:


I had the same problem but also wanted to get the content of the file (after the YAML front-matter).

This is the best solution I have found:

if (md = contents.match(/^(?<metadata>---\s*\n.*?\n?)^(---\s*$\n?)/m))
  self.contents = md.post_match
  self.metadata = YAML.load(md[:metadata])
end

Source and discussion: https://practicingruby.com/articles/tricks-for-working-with-text-and-files

Module: YAML (Ruby 2.5.1), In Files. yaml.rb; yaml/dbm.rb; yaml/store.rb This module provides a Ruby interface for data serialization in YAML format. You can still use Syck, if you prefer, for parsing and emitting YAML, but you must install the 'syck' gem now in order to� First off, your yaml file is not correct YAML, you cannot have keys like that, if you have space or weirdness in them you need to quote them, and what's up with the : at the beginning?


Here is the one liner i use, from terminal, to test the content of yml file(s):

$ ruby  -r yaml -r pp  -e 'pp YAML.load_file("/Users/za/project/application.yml")'
{"logging"=>
  {"path"=>"/var/logs/",
   "file"=>"TacoCloud.log",
   "level"=>
    {"root"=>"WARN", "org"=>{"springframework"=>{"security"=>"DEBUG"}}}}}

Parsing YAML files on the command line using Ruby, Luckily for me, I also have Ruby available in the particular environment where I needed to parse the YAML file, so doing this was as easy as:. So we have a Hash object as described by the YAML file. Using this Hash is pretty straight forward. Since each company's name and year come in a separate hash held by the outer hash (company1, company2), we can iterate through the companies.


Parsing YAML Documents, Parsing YAML Documents. The YAML::parse method has the same syntax as the YAML::load method. A single IO object or String containing a YAML document is passed in to the method. Rather than returning a native Ruby object, though, the YAML::parse method returns a YamlNode representing the document. YAML and Ruby To use the data from your YAML file in a Ruby program, the first step is to load the YAML library: require 'yaml' Then load the data: config = YAML.load(yaml_string) # From string config = YAML.load_file("config.yml") # From file Then you can access to data just as you would any array of hashes (or whatever your data structure):


module Psych - Documentation for Ruby 2.1.0, I NEED TO PARSE OR EMIT YAML RIGHT NOW! YAML Parsing; YAML Emitting; High-level API; Parsing; Reading from a string; Reading from a file; Exception� In older Ruby versions, ie. <= 1.9, Syck is still provided, however it was completely removed with the release of Ruby 2.0.0. More info ¶ ↑ For more advanced details on the implementation see Psych, and also check out yaml.org for spec details and other helpful information.


YAML, YAML is a human-readable data-serialization language. It is commonly used for configuration files and in applications where data is (Though UTF-32 is not mandatory, it is required for a parser to have JSON compatibility.) filtering of YAML files using the line-oriented commands in grep, AWK, Perl, Ruby, and Python. I have a YAML file looks like this:--- !ruby/object:Hi num: 1 --- !ruby/object:Hi num: 2 and my ruby code: require 'yaml' class A attr_accessor :num def initialize num @num=num end end a=A.new 1 b=A.new 2 File.open 'test.yml', 'r+' do |f| f.write YAML.dump a f.write YAML.dump b f.seek(0) #c=obj a #d=ojb b end