Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
While we're talking about serialization formats, we can't forget about YAML. Yaml stands for YAML Ain't Markup Language and is a common serialization format in use today by many Ruby projects. YAML is also a plain text format and you can find parsers for it in many languages besides Ruby. Let's take a look at working with YAML now using Workspaces.
Links
While we're talking about serialization
formats, we can't forget about YAML.
0:00
YAML stands for yet
another markup language, and
0:06
is a common serialization format in
use today by many Ruby projects.
0:09
YAML is also a plain-text format,
and you can find parsers for
0:14
it in many languages besides Ruby.
0:19
Let's take a look at working
with YAML now, using workspaces.
0:23
Now we're gonna take a look at YAML.
0:28
Now we'll have the link to the YAML
documentation right below the video, so
0:29
go ahead and check that out.
0:33
YAML is another markup language, and
the way that YAML is represented
0:35
is usually with three dashes and
then the data type.
0:40
Now let's go ahead and see how that works.
0:44
Ruby includes built in serializers for
most classes to YAML.
0:46
So first we'll have to require
it like we're used to and
0:52
we'll say my_hash = { name: "Jason",
0:55
email: "jason@teamtreehouse.com" }.
1:00
Now, if we wanted to dump this
out using YAML serialization,
1:07
we would say YAML.dump(my_hash).
1:11
And that would be a string representation,
but that's kind of hard to read.
1:15
What this would actually look like with
those escape characters is the following.
1:21
So we have the three dashes and
then the hash key, name and
1:26
hash key email with a colon
separating the data.
1:30
Say my_hash[year] = 2015.
1:35
And you can see we don't need to do
anything special for that to work.
1:38
It will automatically work with
numbers and strings as well.
1:46
Similarly to outputting the YAMLl,
we can load YAML.
1:51
One of the easiest ways to do that
is using the load file method.
1:56
I've prepared an example YAML file,
which has an array of hashes.
2:00
We represent an array with a dash for
each separate item.
2:05
If I scroll down here,
I'm going to clear my screen, and
2:08
we could say YAML.load_file.
2:11
And pass at the name of the file,
which is example.yml.
2:15
And that will return our array of
hashes after reading it from the file.
2:20
And so we could assign that to
a variable if we wanted to.
2:26
And then we would have
access to this people array.
2:35
Similarly, we could call to YAML on
that and we would get a string back,
2:40
so we'll say yaml_output
i= people.to_yaml.
2:46
And just like load file,
we could serialize this back again
2:51
using the load method which takes
a string rather than a file location.
2:55
YAML.load (yaml_output)
returns another Ruby object.
2:59
Now, once again,
security considerations with YAML
3:07
is that you don't wanna load
untrusted data with it.
3:10
It's unsafe and could compromise
the security of your program.
3:13
YAML's very useful for
configuration files,
3:18
A lot of Rubyists have traditionally used
YAML to store application configuration.
3:21
Go ahead and try working with YAML
on your own now using work spaces.
3:27
You need to sign up for Treehouse in order to download course files.
Sign up