Ruby YAML and its Use in Configuring Databases

ruby yamlThe Ruby computer programming language has come into widespread popularity since its creation in the mid-1990s because it’s both easy to understand and equally easy to write. Ruby’s companion web application development technology is Ruby on Rails and it too is very easy to use and write web applications in. The extremely popular social media site Twitter is an example of a Rails-developed web application, as is Basecamp. You can learn all about Ruby and how it interfaces with YAML by signing up for Udemy’s beginners Ruby programming course. Additionally, if you’re just getting started and want to learn more about what Ruby has to offer, Udemy offers a thorough yet concise explanation of Ruby on Rails at its Ruby on Rails blog. Hopeful Ruby programmers should go to it first to see what all the excitement is about.

YAML stands for “Yet Another Markup Language” or, in some parts of the computer program developer world, “YAML Ain’t Markup Language.” The contradiction in whether YAML is or isn’t a classic markup language like HTML is more about the developer’s outlook on YAML than anything else. Most Ruby enthusiasts tend not to think of YAML as an actual markup language.

First developed in 2001, YAML is most often seen in use with Ruby on Rails and it compiles the application development technology’s databases in such a way that the entire activity isn’t really noticeable. Ruby YAML is also what in the Ruby programming is called a “module.”

In contrast to traditional computer languages that use data input and logic to perform tasks, Ruby programs utilize objects written in lines of code, classes and modules, making Ruby an “object-oriented language” or OOP. Check out Udemy’s complete Ruby course bundle, where the relationship between Ruby, Ruby on Rails and YAML is clearly explained in a way that allows it to be easily understood.

Ruby YAML and Data Serialization

Ruby class objects “inherit” or receive data or information about the performance of tasks from Ruby modules such as Ruby YAML. The YAML module simply provides a Ruby interface for the serialization of data in the specific format utilized by YAML. Data serialization itself is nothing more than the process that translates data structures or an object state, such as the objects in Ruby, into a format that can be stored and then brought back up, or “resurrected,” later.

As far as data serialization goes the activity in the Ruby YAML module allows an object state to be resurrected in the same or yet another computer environment. In Ruby on Rails, for example, the Ruby YAML module converts the object that actually performs a task or series of tasks into a stream of data and then stores it securely away in any medium desired, such as a memory stick.

That memory stick loaded with the Ruby on Rails data stream in the form of the Ruby on Rails application can then reconstitute or recreate itself by “deserialization.” When a Ruby object is deserialized, it’s restored to its original state. Ruby YAML makes data serialization, deserialization and reconstitution very easy. Udemy offers both basic Ruby programming training as well as a very useful Ruby on Rails for beginners training course that quickly brings a learner up to speed on data serialization and its uses in computer programs and web applications.

Types of Ruby Data

There are two basic types of data when it comes to Ruby, Ruby on Rails and Ruby YAML: the kind of data that is inserted into a hash and the kind that goes into an array. Here’s an example:

name:  value

–   value

In the example above you can combine the syntax to make fairly complex structures of data, or data structures. Here’s an example of an array composed of hashes:

name:  April

age:   22

name:  Tom

age:   32

YAML allows you to enter all your hashes as they’d look in Ruby itself:

–  { name:  April,  age:  22 }

–  { name:  Tom,  age:  32 }

You can use YAML to enter your arrays as they’d also look in Ruby, called an “array of arrays”:

– [April, 22}

– {Tom, 32]

The term “hash” is often found in Ruby on Rails web applications such as Twitter, where “pound Ruby on Rails” would be expressed as #RubyOnRails.” YAML Comments start with a hash, or #, just as they would in Ruby and its Ruby on Rails, with Ruby YAML able to guess at the specific type of format.

Using Ruby YAML, strings or lines of inputted characters would be enclosed by single-quotes or by double-quotes to allow what’s called an “escape sequence.” Or strings can be enclosed by noting at all; it’s all up to the developer writing the particular Ruby-based program.

In Ruby YAML, data that’s repeated can be replaced by what’s known as an “anchor” simply by labeling the first appearance or occurrence of that data. YAML labels always begin with an ampersand, or &, and any references in the program to that label with an asterix *.

Using Ruby YAML you only need to use the character | or the character > for data that’s going to go on to more than one line of code. The character | will preserve any line breaks while the character > won’t, and it’s all similar to classic HTML or “hyper-text markup language” in look and feel. Here’s an example of Ruby YAML that illustrates all of the discussed characters as they’d be used in Ruby:

name: April         # A string
:age: 22           # An integer
:male: true        # A boolean
:born: 1992-02-29  # A date
:address: &add
213 Main Street
Big City
England
:comment:
This is a potentially very
long comment that will be
just one line long.

:name: Tom
:age: 32
:address: *add
:comment: ‘A very short comment

Loading Ruby YAML Module

Ruby YAML is a part of the very large standard library of Ruby modules that can be loaded into a Ruby program. To utilize YAML in Ruby you must first load it into the specific Ruby program you’re developing or writing by typing in the following into your onscreen Ruby console: require ‘yaml’.

Next, you simply load whatever data it is you have stored within your Ruby YAML module:

config = YAML.load(yaml_string)       # From string
config = YAML.load_file("config.yml") # From file

The above YAML loading entry would allow you to access your stored data with any array of hashes or whatever other way in which you’ve structured your data in YAML. The point is, YAML is really easy to use, not only in Ruby but also in Ruby on Rails, or just “Rails.” And the beauty of Ruby YAML is that it’s already a part of Rails so there’s no need to use the ‘require’ entry as you’d have to do to load YAML into the program you’re developing, or have already written, in Ruby.

Udemy offers several basic Ruby training courses that discuss YAML and show how it works within Ruby. Plus, basic and advanced Ruby on Rails training courses from Udemy can teach you the ins and outs of YAML and how it can be used to reduce program writing time as well as the effort needed in writing a Ruby or Rails program. If you’ve finally decided it’s time to take your first computer programming steps there’s no better programming language to learn than Ruby so check out Udemy now for all its Ruby courses.