Reading YAML in Ruby~(For Real Dummies That are Just Like ME!)

Nana Lau
5 min readDec 2, 2020

Welcome to anyone who decided to read this very first blog I have ever wrote. This blog is going to talk about how I (the dummy) load a .yml file within Ruby and have it outputs to a pretty hash.

I’m going to start with a funny joke to make this blog enjoyable to read.

Back in 2007, my Dad bought a Mac Mini home. He said I can play around with it. Before the Mac Mini, I have only owned Windows computers. I didn’t know much about Apple’s computer before so I thought well why not try it out? Since my Dad claimed that it is the current best computer in the world during that time. (my Dad is a tech geek) Therefore, I went ahead and play around with it.

The first few days with the Mac Mini was pretty much just clicking around anywhere that I can click because nobody really taught me how to use all those features in there. After a while, I got bored with the Mac Mini(there were no games in there and many websites that I use don’t display properly with the Mac Mini back in the days), I ended up deciding to try to clean up my desktop. Nothing much really existed on the desktop except the hard disk icon. So I went ahead and drag that hard disk icon into the trash, and guess what? Taadaa~!! It never turns on again! Just like what this lovely grandpa did with his computer.

So, if you are a computer dummy just like me, or you are also a Ruby newbie who doesn’t know what is going on most of the time, I believe this blog will be able to walk you through the process of loading a YAML file into Ruby and outputs that information in pretty hashes with ease.

Let’s get started! I first started by putting the YAML file(emoticons.yml) into my lib folder. The lib folder is where you write the main body of your Ruby code in your project. I also have a translator.rb ruby file along with the emoticons.yml file, which is where we will be writing some code to load and manipulate the information within the emoticons.yml file.

Both the emoticons.yml and translator.rb are placed in the lib folder
Having both the emoticons.yml and translator.rb file in the lib folder

This is what it looks like in the emoticons.yml file, where I have the emoticons listed under their meaning. Each meaning comes with an English version of the emoticon, and a Japanese version of the emoticon. (I love Japanese emoticons!)

”ヽ(o`皿′o)ノ”

A list of emoticons written in YAML style
List of emoticons written in YAML format

In my translator.rb file, at the very top of my file, I have ‘require “yaml”’ to let ruby knows we will be working with some YAML file.

require “yaml” at the top of the translator.rb file
require “yaml” at the top of the translator.rb file

Afterward, in order to load the YAML file, we will need to define a method #load_library where it accepts an argument of a file path of where your YAML file belongs. In this case, it would be ‘./lib/emoticons.yml’. In order to load a YAML file, you will need to input ‘YAML.load_file(file_path)’ in your #load_library method in order for ruby to knows you need to load a YAML file. I have my file path(‘./lib/emoticons.yml’) set inside of the load_file parenthesis to have the file loaded by ruby. And I will save it into a variable for easy access in the next few lines.

write a method of #load_library with file_path as an argument, and have the file name saved to a variable
write a method of #load_library with file_path as an argument, and have the file name saved to a variable

After loading the emoticons.yml file, we will need to iterate over the list that was loaded by ‘YAML.load_file(file_path)’ to create a readable, manageable hash in the form of Ruby. If we do not iterate and make some changes to the loaded information, the emoticons will be looking like this:

The structure of the emoticons before iteration

And this is not the exact thing that I want… It looks pretty messy and I can’t really tell which one is which when the emoticons are in the same array.

Therefore, in #load_library, I followed with an enumerable block where the name(meaning) is key and the emote is value. I used the path variable I set before and using the [name(meaning)] as key and set it into an empty hash. Then I followed by an English and Japanese key to point to the respective emoticons in their array.

I iterate through the path variable to create the pretty hash that I want

The reason for the emote[0] and emote[1] at the end of the last two lines of code, is because the English and Japanese emoticons are in position [0] and position [1] in their array respectively.

Once those iterations are done, this is what it will look like where each emoticon has a key and it tells the reader whether it is an English emoticon or a Japanese emoticon! Woohoo!

The end result is a pretty hash!

This is how my #load_library looks like when completed. Now you’ve read through my walkthrough, I hope you won’t be as lost as I was during your journey of loading the YAML file into Ruby anymore! (^v^)!!

The complete code in the translator.rb file!

Have a great day everyone! I put an exciting fluffy cat here to make your day delight and joyful! Take care~

--

--

Nana Lau

A real dummy who are trying to learn in a smart way