How Do I Parse a YAML File in Ruby?

When developing a useful application in Ruby, there are numerous circumstances in which we would want to generate some configuration files. For convenience, we want to put our useful application on the client’s server; thus, we must create configuration files. Writing configuration files typically involves using the YAML data serialization language. YAML is an abbreviation that is a mark-up language, not a document. It is frequently employed in programs that store or transport data and configuration files. Data serialization is a feature of the YAML module in Ruby. Extensible Mark-up Language (XML) and YAML are both intended for use in communications, but XML has a more extensive syntax, while YAML has a more condensed grammar.

How To Get YAML in the Ruby Language in Ubuntu 20.04

To parse the YAML file in the Ruby language, we must install it in our system. By executing the following command, we may install YAML in Ruby on Ubuntu 20.04.

$ sudo apt-get update  
$ sudo apt-get install ruby-safe-yaml

After the YAML package is installed onto your computer. The piece of code given below can be used in our application to use the YAML library. We may need to import a YAML file in the language Ruby while dealing with it. The YAML library makes it easy to import a YAML file.

require ‘yaml’

Here are a few instances of loading YAML files with Ruby. The YAML module lets us quickly import any YAML file into our Ruby program.

Example # 1: Parsing a YAML File With the Load File Method in Ruby in Ubuntu 20.04

A YAML stream is “loaded” when converted into native types or vice versa. This might be any Ruby object, including a Hash, an Array, or another type. YAML must first be parsed before it can be loaded into those kinds. At the parsing stage, rather than during native typing, the document’s structure becomes clear.

We have inserted a command “irb” and entered it into the Ruby prompt shell, where we can easily execute the Ruby commands and function. As we have to parse the YAML file in this example, we must create the file first. We have included the library by giving the required YAML command and its returned true value. After this, we have inserted an array of some fruits’ names inside the object fruits. By the method, we have assigned a file name with the YML extension. Inside the method section, we have performed the file.write operation that converts the fruits object to the YAML format.

You can see the content is added inside the fruits.yml file in the YAML format.

Now, to parse the fruits.yml file into the Ruby language, we have utilized the load file method. The object is created as fruit_array, which has the YAML load file method. Inside that method, we have invoked the read function that inputs the fruits.yml file as a parameter. Now, to print the YAML format into Ruby, include the “p fruit_array” command, and the array is generated in a Ruby language format.

Example # 2: Parsing a YAML File for the Complex Data Structure Method in Ruby in Ubuntu 20.04

The process of working with more complicated data structures is the same. A recipe is presented here in the form of a hash that contains an array that itself contains some hashes.

We need to make the YAML file first before Ruby can parse it. The previous code shows how to create the complex data structure into the YAML file. We have added the “YAML” library for accessing the YAML features in the Ruby shell. Then, we have defined an object “recipe”, and inside that object, the hash is created with different and nested fields. With the method, we have opened a file “milkshake.yml” in the write mode option. Then we converted the object “recipe” into the YAML format with the to_YAML extension.

The milkshake.yml file is opened, and the data is successfully inserted inside in YAML format.

Now in the following command, we can easily parse the YAML file into the Ruby language. When the file is retrieved, a new hash containing data identical to the uploaded data is returned.

Example # 3: Parsing a YAML File From the Stream in Ruby in Ubuntu 20.04

Multiple documents may be present in a YAML stream. The complete stream won’t frequently need to be loaded into memory. As an alternative, you should load each document individually. To iterate across documents, we use Ruby’s YAML::load documents function. Assume, for illustration, that we have a log file from a web server that is composed of a stream of various YAML documents:

Now, we are going to parse this file into the Ruby language by the following command:

We have imported the YAML library into our “irb” prompt shell. Then, we have declared a variable “log” where we have opened the “my.yaml” file. Then, we have another object, “yp”. Inside that object, we have deployed the yaml::load_stream and iteratively processed this file’s documents. To the Ruby puts method, we have assigned the fields name from the YAML file that will be parsed into the Ruby format.

Here, we have outputted a summary of each line in the following snapshot:


Now, we are wrapping up the article on parsing the YAML file in the Ruby language. Since YAML is essentially just a data format, there isn’t a need for a YAML library since it is needed to interpret YAML-formatted files. For data serialization in YAML format, this “requires YAML” module offers a Ruby interface. With the YAML load file and YAML parse method, we can quickly parse the YAML file in Ruby. To help you understand, we included three different examples of parsing the YAML file in Ruby.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.